【问题标题】:How to retrieve PHAsset from UIImagePickerController如何从 UIImagePickerController 中检索 PHAsset
【发布时间】:2017-12-12 03:16:37
【问题描述】:

我正在尝试检索 PHAsset,但是 PHAsset.fetchAssets(withALAssetURLs:options:) 已从 iOS 8 中弃用,所以我怎样才能正确检索 PHAsset?

【问题讨论】:

    标签: ios uiimage uiimagepickercontroller photos


    【解决方案1】:

    我有同样的问题,首先检查权限并请求访问:

    let status = PHPhotoLibrary.authorizationStatus()
    
    if status == .notDetermined  {
        PHPhotoLibrary.requestAuthorization({status in
    
        })
    }
    

    只需将它连接到触发您的 UIImagePickerController 的任何东西。委托调用现在应该在 userInfo 中包含 PHAsset。

    guard let asset = info[UIImagePickerControllerPHAsset] as? PHAsset
    

    【讨论】:

    • iOS 11.0 及以上版本可用。低版本 iOS 有什么解决方案吗?
    • 我在模拟器中得到了nil
    • 信息键枚举已更改为更像 Swift。将UIImagePickerControllerPHAsset 替换为UIImagePickerController.InfoKey.phAsset,或仅替换为.phAsset
    • 我在 iPhone 6s 上遇到了这个问题,但在 iPhone XSMax 上没有,在这个建议也适用于 6S 之后,顺便说一句,两部 iPhone 都在最新的 iOS 13.5 固件上
    【解决方案2】:

    这是我的解决方案:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
            if #available(iOS 11.0, *) {
                   let asset = info[UIImagePickerControllerPHAsset]
    
            } else {
                   if let assetURL = info[UIImagePickerControllerReferenceURL] as? URL {
                   let result = PHAsset.fetchAssets(withALAssetURLs: [assetURL], options: nil)
                   let asset = result.firstObject
                   }
           }
    }
    

    【讨论】:

      【解决方案3】:

      除非用户授权,否则 PHAsset 不会出现在 didFinishPickingMediaWithInfo: info 结果中,这对我来说并没有通过显示选择器来实现。我在 Coordinator init() 中添加了这个:

                  let status = PHPhotoLibrary.authorizationStatus()
      
                  if status == .notDetermined  {
                      PHPhotoLibrary.requestAuthorization({status in
      
                      })
                  }
      

      【讨论】:

      • ?这有帮助!您需要请求授权真的不明显,因为选择器出现了,并且即使没有它也会返回图像。
      【解决方案4】:

      我不确定你想要什么。

      您是否尝试以 iOS 8 为目标?

      这就是我获取照片的方式,它适用于 iOS(8.0 及更高版本)、macOS(10.11 及更高版本)、tvOS(10.0 及更高版本)。 代码在可能混淆的地方被注释了

      1. 第一个函数设置获取照片的选项
      2. 第二个函数实际上会获取它们

         //import the Photos framework
         import Photos
         //in these arrays I store my images and assets
         var images = [UIImage]()
         var assets = [PHAsset]()
        
         fileprivate func setPhotoOptions() -> PHFetchOptions{
                let fetchOptions = PHFetchOptions()
                fetchOptions.fetchLimit = 15
                let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
                fetchOptions.sortDescriptors = [sortDescriptor]
                return fetchOptions
            }
        
        
        
        
        fileprivate func fetchPhotos() {
        let allPhotos = PHAsset.fetchAssets(with: .image, options: setPhotoOptions())
        
        DispatchQueue.global(qos: .background).async {
        
            allPhotos.enumerateObjects({ (asset, count, stop) in
                let imageManager = PHImageManager.default()
                let targetSize = CGSize(width: 200, height: 200)
                let options = PHImageRequestOptions()
                options.isSynchronous = true
                imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in
        
                    if let image = image {
                        self.images.append(image)
                        self.assets.append(asset)
        
        
                    }
        
                    if count == allPhotos.count - 1 {
                        DispatchQueue.main.async {
                            //basically, here you can do what you want
                            //(after you finish retrieving your assets)
                            //I am reloading my collection view
                            self.collectionView?.reloadData()
                        }
                    }
        
                })
        
        
            })
        
        }
        

        }

      根据 OP 的说明进行编辑

      需要设置委托UIImagePickerControllerDelegate

      然后实现下面的函数

      func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
      

      在上述方法中,得到这样的图像:

      var image : UIImage = info[UIImagePickerControllerEditedImage] as! UIImage
      

      【讨论】:

      • 我想从 UIImagePickerController 获取的图像中检索 PHAsset
      • 我不敢相信这样做的唯一方法是获取整个照片库,然后枚举每张照片!
      猜你喜欢
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多