元数据(EXIF、TIFF 等)在 Photokit 中的应用

12
在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中(仍然可以使用,可能仍然是向前正确的方法),您只需使用-[ALAssetLibrary writePhotoToSavedPhotosAlbum:metadata:completionBlock:], 它允许您传递元数据字典。

在PhotoKit中是否有等效的方法?

1个回答

10

您可以通过使用临时文件来实现相同的结果。例如:

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
}

我正在从我的项目中复制粘贴代码,希望我没有漏掉什么。


你漏掉了实际将数据保存到文件URL的代码 ;) 另外请注意,相机应用程序将它们保存为JPG而不是PNG。 将其保存为PNG会导致我得到一个4.5 MB的文件,而使用相机拍摄时只有850 KB。 - Jordan H
这将创建新的图像。您知道如何编辑现有照片资源图像的元数据吗? - Premal Khetani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接