将更新的图像保存到PhotoKit时缺少图像元数据

Ray*_*aph 8 core-graphics ios photosframework swift3

我在更新图像的元数据并将其保存回照片库时遇到问题.一切正常,除了改变后的图像元数据缺少之前的条目,我没有得到操纵图像或执行照片库更改块的任何错误.此外,在将字典写回图像之前的字典看起来像原始加上调试器中的字典.

我的问题是:

  1. 我做错了什么会导致重写现有的属性与额外的数据,以消除那里有什么?
  2. 这样做有更好,更规范的方法吗?更新图像中的某些元数据似乎有很多机制.这看起来像是别人在做什么.

编辑:

在保存之前,所有Exif和Tiff值都存在.这是使用以下代码保存到照片后的整个元数据:

["PixelHeight": 2448, "PixelWidth": 3264, "{Exif}": {
ColorSpace = 1;
PixelXDimension = 3264;
PixelYDimension = 2448;}, "Depth": 8, "ProfileName": sRGB IEC61966-2.1, "Orientation": 1, "{TIFF}": {
Orientation = 1;}, "ColorModel": RGB, "{JFIF}": {
DensityUnit = 0;
JFIFVersion =     (
    1,
    0,
    1
);
XDensity = 72;
YDensity = 72;}]
Run Code Online (Sandbox Code Playgroud)

代码全部在Swift 3中,在iOS 10.1上进行测试

基本工作流程是:

    // Get a mutable copy of the existing Exif meta
    let mutableMetaData = getMutableMetadataFrom(imageData: data)

    // Check to see if it has the {GPS} entry, if it does just exit.
    if let _ = mutableMetaData[kCGImagePropertyGPSDictionary as String] {
       callback(imageAsset, true, nil)
       return
    }

    // Add the {GPS} tag to the existing metadata
    let clLocation = media.location!.asCLLocation()
    mutableMetaData[kCGImagePropertyGPSDictionary as String] =
       clLocation.asGPSMetaData()

    // Attach the new metadata to the existing image
    guard let newImageData = attach(metadata: mutableMetaData, toImageData: data) else {
            callback(imageAsset, false, nil)
            return
    }

    let editingOptions = PHContentEditingInputRequestOptions()
    imageAsset.requestContentEditingInput(with: editingOptions) { editingInput, info in
        guard let editingInput = editingInput else { return }
        let library = PHPhotoLibrary.shared()
        let output = PHContentEditingOutput(contentEditingInput: editingInput)
        output.adjustmentData = PHAdjustmentData(formatIdentifier: "Project", formatVersion: "0.1",
                                                 data: "Location Adjustment".data(using: .utf8)!)
        do {
            try newImageData.write(to: output.renderedContentURL, options: [.atomic])
        } catch {
            callback(imageAsset, false, error)
            return
        }

        library.performChanges({
            let changeRequest = PHAssetChangeRequest(for: imageAsset)
            changeRequest.location = clLocation
            changeRequest.contentEditingOutput = output

        }, completionHandler: { success, error in ... ...
Run Code Online (Sandbox Code Playgroud)

工作流的辅助方法是:

func attach(metadata: NSDictionary, toImageData imageData:Data) -> Data? {

    guard
        let imageDataProvider = CGDataProvider(data: imageData as CFData),
        let cgImage = CGImage(jpegDataProviderSource: imageDataProvider, decode: nil,
                              shouldInterpolate: true, intent: .defaultIntent),
        let newImageData = CFDataCreateMutable(nil, 0),
        let type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,
                                                         "image/jpg" as CFString, kUTTypeImage),
        let destination = CGImageDestinationCreateWithData(newImageData,
                                                           (type.takeRetainedValue()), 1, nil) else {

            return nil
        }

    CGImageDestinationAddImage(destination, cgImage, metadata as CFDictionary)
    CGImageDestinationFinalize(destination)

    guard
        let newProvider = CGDataProvider(data: newImageData),
        let newCGImage = CGImage(jpegDataProviderSource: newProvider, decode: nil,
                                 shouldInterpolate: false, intent: .defaultIntent) else {

            return nil
    }

    return UIImageJPEGRepresentation(UIImage(cgImage: newCGImage), 1.0)
}

func getMutableMetadataFrom(imageData data : Data) -> NSMutableDictionary {

    let imageSourceRef = CGImageSourceCreateWithData(data as CFData, nil)
    let currentProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef!, 0, nil)
    let mutableDict = NSMutableDictionary(dictionary: currentProperties!)

    return mutableDict
}
Run Code Online (Sandbox Code Playgroud)

此外,这asGPSMetaData是一个扩展,而CLLocation不是看起来这个Gist的Swift 3版本

Ray*_*aph 7

事实证明,使用CoreGraphics的图像或元数据操作根本不是问题,这是我忽略的几件事:

  1. 从包含EXIF或GPS数据的数据构造UIImage会删除该数据......实际上,除了一组核心JFIF和大小数据(使用JPEG时),它会删除大部分元数据.回想起来它是有道理的,因为它们的内部表示只是原始数据.但是,我没有在文档中找到任何关于元数据的明确声明.
  2. 鉴于先前的观点,将数据(具有元数据的图像)对象获取到照片库中的主要两种方法是将其写入临时文件并使用 PHAssetChangeRequest::creationRequestForAssetFromImage(atFileURL:) 或者使用它PHAssetCreationRequest::forAsset来创建创建请求,然后使用PHAssetCreationRequest::addResource(with:data:options:) 添加数据作为照片.我选择了后者,因为它的运动部件较少.

所以我想所有这些都取代了漂亮,简洁ALAssetsLibrary::writeImage(toSavedPhotosAlbum:metadata:completionBlock:).

照片库的最终更改块最终是这样的:

    var assetID: String?
    PHPhotoLibrary.shared().performChanges({ 
        let creationRequest = PHAssetCreationRequest.forAsset()
        creationRequest.addResource(with: .photo, data: imageWithMetaData as Data, options: nil)
        creationRequest.location = clLocation
        assetID = creationRequest.placeholderForCreatedAsset?.localIdentifier
    }) { success, error  in ...
Run Code Online (Sandbox Code Playgroud)