【问题标题】:Get an error when trying to get all the photos from PHAssetCollection.fetchAssetCollections尝试从 PHAssetCollection.fetchAssetCollections 获取所有照片时出错
【发布时间】:2020-01-08 05:08:10
【问题描述】:

我想获取我的自定义相册的所有照片。 但我得到的是以下错误。

我的代码

let collections:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

我得到的错误

“从守护进程返回错误:错误域=com.apple.accounts 代码=7 “(空)”“

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 首先在 info.plist 中添加 Privacy - Photo Library Usage Descriptionkey 然后尝试运行此代码
  • Xcode 版本 11.0 beta 6 (11M392r) Swift 5
  • 我在终端上打印了这个错误,但 PHAssets 正在被正确检索。但是,当我尝试在 Xcode11 中调用 PHImageManager().requestImage 时,我需要指定 options.deliveryMode = .highQualityFormat,而它过去在 Xcode10 中与 .fastFormat 一起工作得很好。
  • 我在 Xcode 11.5 版上遇到同样的错误
  • 这实际上并不会阻止您获取资产。这可能是 Apple 方面的错误 - 将其提交到 Feedback Assistant.app。

标签: swift photosframework


【解决方案1】:

根据 cmets,我不完全确定问题出在哪里,但我希望这段代码可以提供一些帮助。使用 .album 而不是 .smartAlbum 也可能是问题的一部分。

private var fetchResult: PHFetchResult<PHAsset>!

func fetchOptions(_ predicate: NSPredicate?) -> PHFetchOptions {
            let options = PHFetchOptions()
            options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]
        options.predicate = predicate
        return options
    }
}

    if let userLibraryCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil).firstObject {
        self.fetchResult = PHAsset.fetchAssets(in: userLibraryCollection, options: fetchOptions(NSPredicate(format: "mediaType = \(PHAssetMediaType.image.rawValue)")))
    } else {
        self.fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions(nil))
    }

【讨论】:

    【解决方案2】:
    private var fetchResult: PHFetchResult<PHAsset>!
        
    func picker(_ picker: PHPickerViewController, didFinishPicking 
     results: [PHPickerResult]) {
        self.dismiss(animated: true, completion: nil)
        
        
        if let userLibraryCollection = 
     PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: 
     .smartAlbumUserLibrary, options: nil).firstObject {
            
            self.fetchResult = PHAsset.fetchAssets(in: 
         userLibraryCollection, options: fetchOptions(NSPredicate(format: 
         "mediaType = \(PHAssetMediaType.image.rawValue)")))
            
            fetchResult.enumerateObjects { asset, index, stop in
                PHAsset.getURL(ofPhotoWith: asset) { (url) in
                    
                    if let imgurl = url{
                        print(imgurl)
                    }else {
                        print("error")
                    }
                    
                }
                
            }
            
            
            
        } else {
            self.fetchResult = PHAsset.fetchAssets(with: .image, options: 
         fetchOptions(nil))
            
            print(fetchResult.firstObject)
        }
    }
    
    
    
      extension PHAsset {
    static func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
        if mPhasset.mediaType == .image {
            let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
            options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
                return true
            }
            mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
                if let fullSizeImageUrl = contentEditingInput?.fullSizeImageURL {
                    completionHandler(fullSizeImageUrl)
                } else {
                    completionHandler(nil)
                }
            })
        } else if mPhasset.mediaType == .video {
            let options: PHVideoRequestOptions = PHVideoRequestOptions()
            options.version = .original
            PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
                if let urlAsset = asset as? AVURLAsset {
                    let localVideoUrl = urlAsset.url
                    completionHandler(localVideoUrl)
                } else {
                    completionHandler(nil)
                }
            })
           }
        
        }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      相关资源
      最近更新 更多