如何解决无法将IPTC部分添加到捕获的照片元数据中
这是我的应用程序执行的操作-
- 使用AVFoundation API捕获图像
- 将其保存到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 举报,一经查实,本站将立刻删除。