gpt4 book ai didi

objective-c - Photokit 中的元数据(EXIF、TIFF 等)

转载 作者:太空狗 更新时间:2023-10-30 03:22:05 24 4
gpt4 key购买 nike

在 iOS 8 中,使用新的 PhotoKit,创建新图像的方法是使用 -[PHPhotoLibrary performChanges:completionHandler],这样:

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// TODO set metadata?
} completionHandler:^(BOOL success, NSError *error) {
if(success)
{
// do stuff
}
}];

但是,我找不到任何关于如何在图像上设置元数据的文档。

在旧的 ALAssetLibrary(它仍然有效,并且可能仍然是 future 的“正确”方式)中,您只需使用 -[ALAssetLibrary writePhotoToSavedPhotosAlbum:metadata:completionBlock:],它允许您传入元数据字典。

PhotoKit 中是否有等效的方法?

最佳答案

您可以使用临时文件获得相同的结果。例如:

import Photos
import ImageIO
import MobileCoreServices

PHPhotoLibrary.sharedPhotoLibrary().performChanges ({
let temporaryPath = ... // a path in the App's documents or cache directory
let fileURL = NSURL.fileURLWithPath (temporaryPath)!

// custom UIImagePNGRepresentation function is implemented below
let PNGdataWithMetadata = UIImagePNGRepresentation (photo, copyright: "Image Copyright",
author: "Image Author", description: "Image Description")
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL (fileURL)
}, completionHandler: { (success, error) in
// to-do: delete the temporary file
println ("completion \(success) \(error)")
})

// what the custom UIImagePNGRepresentation function does is create a PNG representation that contains the given metadata. The function assumes all functions return valid data
// to add the metadata to the PNG representation of the image we:
// 1. create the PNG representation and read the default values (these will include things like the image's size)
// 2. add the metadata items (see CGImageProperties Reference for the valid keys)
// 3. create an image destination where the new PNG will be created and assigned the metadata dictionary

public func UIImagePNGRepresentation (image : UIImage, # copyright : String, # author : String, # description : String) -> NSData {
// wrap the valid metadata values in a dictionary
var PNGmetadata = [String : AnyObject] ()
PNGmetadata [kCGImagePropertyPNGCopyright] = copyright
PNGmetadata [kCGImagePropertyPNGAuthor] = author
PNGmetadata [kCGImagePropertyPNGDescription] = description

let metadata = [kCGImagePropertyPNGDictionary as String : PNGmetadata]
return UIImagePNGRepresentation (image, metadata: metadata)
}

public func UIImagePNGRepresentation (image : UIImage, # metadata : [String : [String : AnyObject]]) -> NSData {
let imageSource = CGImageSourceCreateWithData (UIImagePNGRepresentation (image), nil)
let defaultAttributes = CGImageSourceCopyPropertiesAtIndex (imageSource, 0, nil) as NSDictionary
let extendedAttributes = defaultAttributes.mutableCopy () as NSMutableDictionary

for item in metadata {
extendedAttributes [item.0] = item.1
}

var outputPNGdata = NSMutableData ()
let imageDestination = CGImageDestinationCreateWithData (outputPNGdata, kUTTypePNG, 1, nil)
CGImageDestinationAddImageFromSource (imageDestination, imageSource, 0, extendedAttributes)
let success = CGImageDestinationFinalize (imageDestination)
assert (success, "Fail!")

return outputPNGdata
}

我正在从我的项目中复制粘贴代码,希望我没有遗漏任何东西。

关于objective-c - Photokit 中的元数据(EXIF、TIFF 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26208277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com