Swift how to modify exif info in images taken from mobile camera

前端 未结 3 754
半阙折子戏
半阙折子戏 2020-12-08 23:47

I use UIImagePickerController to pick images in my iOS App and I know exif info can be got by info[UIImagePickerControllerMediaMetadata]. But when

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 00:45

    using and mergind some info from other posts, I approached problem using Dictionary in Swift. I used it in captureOutput of AVFounfation callback for AVCapturePhoto:

    func photoOutput(_ output: AVCapturePhotoOutput,
                     didFinishProcessingPhoto photo: AVCapturePhoto,
                     error: Error?) {
    
        //retrieve exif information
        var photoFormatDescription: CMFormatDescription?
        CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, photoPixelBuffer, &photoFormatDescription)
    
        var metadataAttachments: Dictionary = photo.metadata as Dictionary
    
        if var exifData = metadataAttachments["{Exif}"] as? [String: Any] {
            exifData[kCGImagePropertyExifUserComment as String] = ""
    
        metadataAttachments[kCGImagePropertyExifDictionary as String] = exifData
        }
    
    }
    

    After that "metadataAttachments" is used to build final image (using CGImageDestinationAddImage in my case)

    It seems to work (tried in a project build with Swift 4.0)

    Hope it can help!

提交回复
热议问题