PHAssetResource.assetResources使用有效的PHAsset返回零结果

时间:2019-02-16 22:07:09

标签: ios swift phasset photokit

我正在尝试获取用于PHAsset的资产资源,并且遇到了一些麻烦。我可以毫无问题地获取PHAsset,但是资产的资源始终为空。并不是每张照片都发生这种情况,但是尤其是一张照片一直在给我带来麻烦。很好奇我是否缺少某些东西,这在某些情况下是预期的行为。

import UIKit
import Photos
import PhotosUI

class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        requestPhotoLibraryAuthorization() { success in
            let identifier = "E218D471-E616-4E39-99C3-624D13D9B661/L0/001"
            let assets = self.fetchPHAssets(identifiers: [identifier])
            guard assets.count > 0 else { return }

            let imageOptions = PHImageRequestOptions()
            imageOptions.deliveryMode = .highQualityFormat
            imageOptions.isNetworkAccessAllowed = true

            PHImageManager.default().requestImage(for: assets[0], targetSize: PHImageManagerMaximumSize, contentMode: .default, options: imageOptions) { (image, info) in
                print(String(describing: info)) // never called
            }

            self.saveAssetToDisk(asset: assets[0]) { image in
                guard let image = image else { return }
                self.imageView.image = image
            }
        }
    }

    func fetchPHAssets(identifiers: [String]) -> [PHAsset] {
        let options = PHFetchOptions()
        options.includeAllBurstAssets = false
        options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        options.fetchLimit = identifiers.count
        options.includeAssetSourceTypes = [.typeCloudShared, .typeiTunesSynced, .typeUserLibrary]

        let assetResult = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: options)
        var assetArray:[PHAsset] = []
        for i in 0..<assetResult.count {
            let asset = assetResult[i]
            assetArray.append(asset)
        }
        return assetArray
    }

    func saveAssetToDisk(asset: PHAsset, onComplete: @escaping (UIImage?)->()) {
        let resources = PHAssetResource.assetResources(for: asset)

        guard resources.count > 0 else { return }

        let resource = resources[0]

        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentUrl = urls[0]
        let resourceUrl = documentUrl.appendingPathComponent(resource.originalFilename, isDirectory: false)

        // write image
        let options = PHAssetResourceRequestOptions()
        options.isNetworkAccessAllowed = true

        if !FileManager.default.fileExists(atPath: resourceUrl.path) {
            PHAssetResourceManager.default().writeData(for: resource, toFile: resourceUrl, options: options) { error in
                if error == nil {
                    guard let image = UIImage(contentsOfFile: resourceUrl.path) else { return }
                    onComplete(image)
                }
            }
        } else {
            onComplete(UIImage(contentsOfFile: resourceUrl.path))
        }
    }

    let requestPhotoLibraryKey = "hasRequestedPhotoAccess"
    func requestPhotoLibraryAuthorization(onComplete: @escaping (Bool) -> ()) {
        let status = PHPhotoLibrary.authorizationStatus()
        if status == .notDetermined {
            PHPhotoLibrary.requestAuthorization { status in
                if status == PHAuthorizationStatus.authorized {
                    UserDefaults.standard.set(true, forKey: self.requestPhotoLibraryKey)
                    onComplete(true)
                }
            }
        } else if status == .authorized {
            UserDefaults.standard.set(true, forKey: requestPhotoLibraryKey)
            onComplete(true)
        } else if status == .denied {
            onComplete(false)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

没有看到Photos Framework requestImageDataForAsset occasionally fails中描述的错误消息,但是我能够找到问题的图像是iCloud共享相册的一部分。按照他们的建议,解决了我的问题。