2017-04-05 49 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