微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将IPTC部分添加到捕获的照片元数据中

如何解决无法将IPTC部分添加到捕获的照片元数据中

这是我的应用程序执行的操作-

  1. 使用AVFoundation API捕获图像
  1. 向其添加新的元数据标签(使用AVCapture FileDataRepresentation())
  1. 将其保存到iPhone的照片库(使用PHPhotoLibrary creationRequest)

我陷入2),因为新添加的元数据将被忽略,并且不会显示在保存的图像中。我要添加的元数据是一个新的“ IPTC”部分,带有“关键字”键,值对。 IPTC部分不存在于原始捕获的照片的元数据中。尝试修改现有的部分(例如Exif关键字)效果很好。

具体问题似乎是由fileDataRepresentation(with:AVCapturePhotoFileDataRepresentationCustomizer)->数据返回的数据?通过我的Customizer函数。 AVCapturePhotoFileDataRepresentationCustomizer内部的打印显示IPTC字典已正确创建。 (见下文)

调用fileDataRepresentation()之后打印photo.Metadata并不会显示它。

我尽全力!任何指针都将不胜感激:)


class PhotoCaptureProcessor : NSObject {
//The data resulting from the photo capture
private var photoData: Data?

//The tagName to add to IPTC Keywords
private var tagName : String

//Process the Captured photo
func photoOutput(_ output: AVCapturePhotoOutput,didFinishProcessingPhoto photo: AVCapturePhoto,error: Error?) {
        ...
        
        if let error = error {
            print("Error capturing photo: \(error)")
        } else {
            //Write the Metadata to the photo data
             self.photoData = photo.fileDataRepresentation(with: PSFileDataRepresentationCustomizer(tagName: tagName))

             //Print metedata Dict after change
             print(photo.MetaData)
        }
        ...
}

//Save the photo to Photo Library with the modified Metadata
func photoOutput(_ output: AVCapturePhotoOutput,didFinishCaptureFor resolvedSettings: AVCaptureResolvedPhotoSettings,error: Error?) {

...
        
        guard let photoData = photoData else {
            print("No photo data resource")
            didFinish()
            return
        }
        
        //The localIdentifier for the saved photo
        var imageIdentifier: String?
        
        PHPhotoLibrary.requestAuthorization { status in
            ...
                //Save the photo
                PHPhotoLibrary.shared().performChanges({
                    
                    let options = PHAssetResourceCreationoptions()
                    let creationRequest = PHAssetCreationRequest.forAsset()
                    options.uniformTypeIdentifier = self.requestedPhotoSettings.processedFileType.map { $0.rawValue }
                    
                    let placeholder = creationRequest.placeholderForCreatedAsset
                    imageIdentifier = placeholder?.localIdentifier
                    
                    
                    //Add the photoAsset to the Photo library
                    creationRequest.addResource(with: .photo,data: photoData,options: options)
                        
                    //Now add the photo to the album
                    let request = PHAssetCollectionChangeRequest(for: album)
                    request?.addAssets([placeholder!] as NSArray)
                    
                    ...
                    }
                    
                },completionHandler: { success,error in
                    ...
                }
                )
            } else {
                self.didFinish()
            }
        }
    }

}

//The Customizer for fileDataRepresentation that adds the Metadata to the photo Data
class PSFileDataRepresentationCustomizer: NSObject,AVCapturePhotoFileDataRepresentationCustomizer {
    
    let tagName : String
    
    init(tagName : String) {
        self.tagName = tagName
        super.init()
    }
    
    public func replacementMetadata(for photo: AVCapturePhoto) -> [String : Any]? {
        return getDict(avFoundationMetadata: photo.Metadata as NSDictionary) as! [String : Any]
        
    }
    
    func getDict(avFoundationMetadata: NSDictionary)->NSMutableDictionary {
     
        let inDict: NSMutableDictionary = (avFoundationMetadata).mutablecopy() as! NSMutableDictionary
        var iptc = inDict[kCGImagePropertyIPTCDictionary] as? [CFString: Any] ?? [:]
        
        iptc[kCGImagePropertyIPTCKeywords] = String(tagName)

        inDict[kCGImagePropertyIPTCDictionary] = iptc
        
        //Print Dict Before
        print(inDict)
        
        return inDict
    }
}


//Print Dict Before shows the Keywords added

{
    DPIHeight = 72;
    DPIWidth = 72;
    Orientation = 6;
    kCGImageDestinationICCProfile = {length = 548,bytes = 0x00000224 6170706c 04000000 6d6e7472 ... 000003dc 0000c06e };
    "{Exif}" =     {
        ...
    };
    "{IPTC}" =     {
        Keywords = laptop;
    };
    "{MakerApple}" =     {
    ...
}

//Print Dict After does not

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?