Swift: Custom camera save modified metadata with image

后端 未结 2 1065
你的背包
你的背包 2020-12-08 17:41

I am trying to save SOME of the metadata from an image sample buffer along with the image.

I need to:

  • Rotate the image to the orientation from the met
2条回答
  •  执念已碎
    2020-12-08 18:03

    I made a greatly simplified version of the code above. It does make an image file, but as Carlos has noted, no custom metadata is in the file when you load it up again. According to other threads, this simply may not be possible.

    func saveImage(_ image: UIImage, withMetadata metadata: NSMutableDictionary, atPath path: URL) -> Bool {
        guard let jpgData = UIImageJPEGRepresentation(image, 1) else {
            return false
        }
        // make an image source
        guard let source = CGImageSourceCreateWithData(jpgData as CFData, nil), let uniformTypeIdentifier = CGImageSourceGetType(source) else {
            return false
        }
        // make an image destination pointing to the file we want to write
        guard let destination = CGImageDestinationCreateWithURL(path as CFURL, uniformTypeIdentifier, 1, nil) else {
            return false
        }
    
        // add the source image to the destination, along with the metadata
        CGImageDestinationAddImageFromSource(destination, source, 0, metadata)
    
        // and write it out
        return CGImageDestinationFinalize(destination)
    }
    

提交回复
热议问题