Cesare Piersigilli
Cesare Piersigilli

Reputation: 407

Swift 5 How to add exif data to image get from PHAsset

I have this simply code to get an uiImage from Photo Library:

private func getDataOfMedia (asset:PHAsset) -> Data {
        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true
        requestOptions.isNetworkAccessAllowed = true

        let imgManager = PHImageManager.default()
        var imgData = Data()
        // Request Image
        imgManager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .default, options: requestOptions) { (uiimage, info) in
            if let uiimage = uiimage {
                DispatchQueue.main.async {
                    if let imageData = uiimage.jpegData(compressionQuality: 1) {
                        imgData = imageData
                    }
                }
            }
        }
        return imgData
    }

but, of course, I can't see the camera data, location data and exif data of the image saved from func getDataOfMedia(asset: asset), but if I download the same image directly from Photo, I can see camera data, location data and exif data. How can I do to add camera data, location data and exif data to data that I have from requestimage of PHAsset? How can I add the unique id, for example asset.localIdentifier, to know that I have download this image?

UPDATE I managed to extract camera data, location data and exif data from an image from an asset object, with this code:

private func getDataOfImageC (asset:PHAsset, completion: @escaping (Data) -> Void) {
        //For get exif data
        let options = PHContentEditingInputRequestOptions()
        options.isNetworkAccessAllowed = true //download asset metadata from iCloud if needed        
        asset.requestContentEditingInput(with: options) { (contentEditingInput: PHContentEditingInput?, _) -> Void in
            let fullImage = CIImage(contentsOf: contentEditingInput!.fullSizeImageURL!)
            let image = UIImage(ciImage: fullImage!)
            print(fullImage!.properties)
            for (key, value) in fullImage!.properties {
                print("key: \(key) - value: \(value)")
            }
            completion(image.jpegData(compressionQuality: 1)!)
        }
    }

but turning CIImage to UIImage to Data format, to save it locally, it loses all camera data, location data and exif data. I hope that someone help me.

Upvotes: 1

Views: 2113

Answers (1)

Cesare Piersigilli
Cesare Piersigilli

Reputation: 407

After some study, this the code that work for me to save image (not video) from photoLibrary with all properties.

private func saveDataOfImageCI (asset:PHAsset, urlMedia: URL) {
        //For get exif data
        let options = PHContentEditingInputRequestOptions()
        options.isNetworkAccessAllowed = true //download asset metadata from iCloud if needed
        asset.requestContentEditingInput(with: options) { (contentEditingInput: PHContentEditingInput?, _) -> Void in
            let url = contentEditingInput!.fullSizeImageURL!
            let fullImage = CIImage(contentsOf: contentEditingInput!.fullSizeImageURL!)
            do {
                try CIContext().writeJPEGRepresentation(of: fullImage!,
                                                        to: urlMedia,
                                                        colorSpace: (fullImage?.colorSpace)!)
            } catch {
                print("error: \(error.localizedDescription)")
            }
            //To print properties data (exif, camera data, ....)
            for (key, value) in fullImage!.properties {
                print("key: \(key) - value: \(value)")
            }

        }
    }

Upvotes: 2

Related Questions