2017-04-05 76 views
0

我加入EXIF数据到用下面的代码iPhone设备上的图像:试图完成目标数据源(CGImageDestinationFinalize)时添加EXIF数据到巨大图像在iOS

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 
} 

方法崩溃。

由于内存压力,应用程序终止。这种情况发生在巨大的图像上(20,000×20,000像素)。普通图像通过这种方法很好。

有没有任何方法可以将EXIF信息添加到图像而不会造成内存压力?

谢谢!

回答

0

CGImageSource被设计为用作数据源而不必一次将所有数据加载到存储器中。而不是使用CGImageSourceCreateWithData,请使用CGImageSourceCreateWithURL(_:_:)从磁盘上的文件创建图像源。系统将根据需要管理数据流,而无需一次将所有数据加载到内存中。

+0

谢谢!刚刚尝试过。遗憾的是,这并没有帮助。 – UrK

相关问题