EXIF的PHContentEditingInputRequestOptions阻止主线程

时间:2017-03-03 15:22:08

标签: ios swift multithreading grand-central-dispatch exif

我正在获取图像EXIF以获取LensMake。

这是我发现这样做的唯一方法:

let lstAssets : PHFetchResult<PHAsset> = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
lstAssets.enumerateObjects({object , index, stop in
    let options = PHContentEditingInputRequestOptions()
    object.requestContentEditingInput(with: options) { (contentEditingInput: PHContentEditingInput?, _) -> Void in
        let fullImage : CIImage = CIImage(contentsOf: contentEditingInput!.fullSizeImageURL!)!
        if let exif = fullImage.properties["{Exif}"]{ ...

问题是requestContentEditingInput的callback / clasure / completion中的代码在主线程中运行,并阻止来自UI的任何调用(例如,按钮的IBAction)。因此,如果我以lstAssets.count为100开始并在enumerateObjects中发出100个请求,则需要完成所有回调以在UI中执行某些操作。

我尝试将enumerateObjects放在DispatchQueue.global(qos:.background).async中没有成功。如果我将回调代码放在后台线程中,也会发生同样的情况。

奇怪的是,如果我有PHImageManager.default()和图像的requestImageData,这种行为是不同的,但问题是我无法以这种方式获得EXIF。

如何取消阻止队列?有没有更好的方法来获得EXIF?

我在Xcode 8.2中运行Swift 3.

更新:GCD的奇怪行为:在PHContentEditingInputRequestOptions解锁主线程之前,在enumerateObjects内运行一个requestImageData,但是当通过点击进入下一个屏幕时,UI等待回调结束。

2 个答案:

答案 0 :(得分:4)

如果您只需要阅读exif元数据,则可以使用PHImageManager,它对背景队列更友好。 因此,您可以将代码更改为以下内容:

let operationQueue = OperationQueue()

let lstAssets : PHFetchResult<PHAsset> = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
operationQueue.addOperation {
    self.readPhotosMetadata(result: lstAssets, operationQueue: operationQueue)
}

func readPhotosMetadata(result: PHFetchResult<PHAsset>, operationQueue: OperationQueue) {
    let imageManager = PHImageManager.default()
    result.enumerateObjects({object , index, stop in
        let options = PHImageRequestOptions()
        options.isNetworkAccessAllowed = true
        options.isSynchronous = false
        imageManager.requestImageData(for: object, options: options, resultHandler: { (imageData, dataUTI, orientation, info) in
            operationQueue.addOperation {
                guard let data = imageData,
                    let metadata = type(of: self).fetchPhotoMetadata(data: data) else {
                        print("metadata not found for \(object)")
                        return
                }
                print(metadata)
            }
        })
    })
}

static func fetchPhotoMetadata(data: Data) -> [String: Any]? {
    guard let selectedImageSourceRef = CGImageSourceCreateWithData(data as CFData, nil),
        let imagePropertiesDictionary = CGImageSourceCopyPropertiesAtIndex(selectedImageSourceRef, 0, nil) as? [String: Any] else {
        return nil
    }
    return imagePropertiesDictionary

}

答案 1 :(得分:0)

我们可以通过更少的代码和更快的方式以这种方式从图像中读取Exif:

let firstAsset = PHAsset.fetchAssets(with: .image, options: nil).firstObject!
// For example I've used first PHAsset

firstAsset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (eidtingInput, info) in
                if let input = eidtingInput, let imgURL = input.fullSizeImageURL {
                    let img = CIImage(contentsOf: imgURL)!
                    let imgPro = img.properties
                    let exifDic = imgPro["{Exif}"] as! NSDictionary
                    print(exifDic)
                  }
               }