【问题标题】:Adding EXIF data to huge image on iOS将 EXIF 数据添加到 iOS 上的巨大图像
【发布时间】:2017-08-30 23:13:22
【问题描述】:

我正在使用以下代码向 iPhone 设备上的图像添加 exif 数据:

func testPhoto(identifier: String) {
    let result =
        PHAsset.fetchAssets(withLocalIdentifiers: [identifier], options: nil)

    PHCachingImageManager
        .default()
        .requestImageData(for: result.firstObject!, options: nil) {
            (data, uti, orientation, info) in
            _ = self.addExif(data: data!)
    }
}

func addExif(data: Data) -> NSData? {
    let selectedImageSourceRef =
        CGImageSourceCreateWithData(data as CFData, nil)!

    let imagePropertiesDictionaryRef =
        CGImageSourceCopyPropertiesAtIndex(selectedImageSourceRef, 0, nil)

    var imagePropsDictionary: [String: Any] =
        imagePropertiesDictionaryRef as! [String : Any]

    var exifData = [String: Any]()

    let newImageData = NSMutableData()

    let imageDestination =
        CGImageDestinationCreateWithData(newImageData, kUTTypeJPEG, 1, nil)!

    exifData[kCGImagePropertyExifDateTimeOriginal as String] = NSDate()

    imagePropsDictionary[kCGImagePropertyExifDictionary as String] = exifData

    CGImageDestinationAddImageFromSource(
        imageDestination,
        selectedImageSourceRef,
        0,
        imagePropsDictionary as CFDictionary)

    if !CGImageDestinationFinalize(imageDestination) {
        NSLog("Could not finalize")
    }

    return newImageData
}

该方法在尝试完成目标数据源 (CGImageDestinationFinalize) 时崩溃。

应用程序因内存压力而终止。这发生在大图像(20,000x20,000 像素)上。普通图片通过这个方法就好了。

有没有什么办法可以在不造成内存压力的情况下给图片添加EXIF信息?

谢谢!

【问题讨论】:

    标签: ios image exif core-image photos


    【解决方案1】:

    CGImageSource 旨在用作数据源,而无需一次将所有数据加载到内存中。不要使用CGImageSourceCreateWithData,而是使用CGImageSourceCreateWithURL(_:_:) 从磁盘上的文件创建图像源。然后,系统将根据需要管理数据流,而无需一次将其全部加载到内存中。

    【讨论】:

    • 谢谢!刚试过。很遗憾,这没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2010-09-18
    • 2020-08-20
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2018-03-12
    • 2020-05-08
    • 2020-06-26
    相关资源
    最近更新 更多