Swift 5如何将exif数据添加到从PHAsset获取的图像中

问题描述 投票:1回答:1

我有这个简单的代码可以从照片库获取uiImage:

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
    }

但是,当然,我看不到从func getDataOfMedia(asset: asset)保存的图像的相机数据,位置数据和exif数据,但是如果我直接从Photo下载同一图像,则可以看到相机数据,位置数据和exif数据。如何将相机数据,位置数据和exif数据添加到我从PHAsset的requestimage获得的数据中?如何添加唯一的ID,例如asset.localIdentifier,以知道我已下载此图像?

UPDATE我使用以下代码从资产对象的图像中提取了相机数据,位置数据和exif数据:

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)!)
        }
    }

但是将CIImage转换为UIImage转换为Data格式,以将其保存在本地时,它将丢失所有相机数据,位置数据和exif数据。我希望有人帮助我。

swift exif phasset
1个回答
0
投票

经过一番研究,这是对我有用的代码,用于从具有所有属性的photoLibrary中保存image(不是视频)。

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)")
            }

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.