Editing Photo metadata and PHAdjustmentData

拥有回忆 提交于 2019-12-04 04:07:55

问题


The following function loads a photo, edits the exif metadata attached to it and saves it back out. The function only seems to work for photos that already have a PHAdjustmentData attached ie photos that have been edited with another application previously. If a photo hasn't been edited it fails in the performChanges() block and prints

Failed to save. Error: Optional(Error Domain=NSCocoaErrorDomain Code=-1 "(null)"). 

Why does it fail in this situation? Looking through Stack Overflow I have seen a number of other versions of this question but none of them seemed to get resolved. I know this fails if the image saved is a PNG but my original image is a JPEG so the saved image is also a JPEG.

func editPhotoProperties(_ asset: PHAsset) {

    let options = PHContentEditingInputRequestOptions()
    options.canHandleAdjustmentData = { data in
            return false
    }
    asset.requestContentEditingInput(with: options) { input, info in
        if let input = input {
            let adjustmentData = PHAdjustmentData(formatIdentifier:"viewfinder", formatVersion:"1.0", data:"viewfinder".data(using:.utf8)!)
            let output = PHContentEditingOutput(contentEditingInput:input)
            output.adjustmentData = adjustmentData

            do {
                let imageData = try Data(contentsOf:input.fullSizeImageURL!)
            } catch {
                print("Failed to load data")
                return
            }

            let properties = getImageDataProperties(imageData)!
            let properties2 = properties.mutableCopy() as! NSMutableDictionary
            // edit properties2
            ...
            let newImageData = addImageProperties(imageData: imageData, properties: properties2)

            do {
                try newImageData!.write(to: output.renderedContentURL, options: .atomic)
            } catch {
                print("Failed to write to disk")
                return
            }           
            PHPhotoLibrary.shared().performChanges({
                let changeRequest = PHAssetChangeRequest(for:asset)
                changeRequest.contentEditingOutput = output
            }) { success, error in
                if !success {
                    print("Failed to save. Error: \(String(describing:error))")
                }
            }
        }
    }
}

func getImageDataProperties(_ data: Data) -> NSDictionary? {
    if let imageSource = CGImageSourceCreateWithData(data as CFData, nil) {
        if let dictionary = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) {
            return dictionary
        }
    }
    return nil
}

// add image properties (exif, gps etc) to image
func addImageProperties(imageData: Data, properties: NSDictionary?) -> Data? {

    // create an imagesourceref
    if let source = CGImageSourceCreateWithData(imageData as CFData, nil) {
        // this is of type image
        if let uti = CGImageSourceGetType(source) {

            // create a new data object and write the new image into it
            let destinationData = NSMutableData()
            if let destination = CGImageDestinationCreateWithData(destinationData, uti, 1, nil) {

                // add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
                CGImageDestinationAddImageFromSource(destination, source, 0, properties)
                if CGImageDestinationFinalize(destination) == false {
                    return nil
                }
                return destinationData as Data
            }
        }
    }
    return nil
}

来源:https://stackoverflow.com/questions/48255636/editing-photo-metadata-and-phadjustmentdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!