简体   繁体   English

从 UIImagePickerController 获取 PHAsset

[英]get PHAsset From UIImagePickerController

I am tring to get PHAsset from UIImagePickerController and using following code:我想从 UIImagePickerController 获取 PHAsset 并使用以下代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

     //   NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
        PHAsset *phAsset = [info valueForKey:UIImagePickerControllerPHAsset];
        if (phAsset) {
            [assetsArr addObject:phAsset];
            [galleryArr addObject:[info valueForKey:UIImagePickerControllerOriginalImage]];
        }

        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self.galleryCollectionView reloadData];
            [picker dismissViewControllerAnimated:YES completion:nil];
        });
    });


}

If source type is UIImagePickerControllerSourceTypePhotoLibrary then I am able to get PHAsset by above code.如果源类型是UIImagePickerControllerSourceTypePhotoLibrary那么我可以通过上面的代码获得 PHAsset。 But if source type is UIImagePickerControllerSourceTypeCamera then PHAsset is nil.但是如果源类型是UIImagePickerControllerSourceTypeCamera则 PHAsset 为零。

How can i get PHAsset in that case?在这种情况下我如何获得 PHAsset?

If UIImagePickerControllerReferenceURL have some value then you can get asset using following code: 如果UIImagePickerControllerReferenceURL具有某些值,则可以使用以下代码获取资产:

PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:imageURL] options:nil];

It may have more than one asset select according to your requirement. 根据您的要求,它可能有多个资产可供选择。

if you get the image from taking photo, you can get the origin image by Info Dictionary如果您通过拍照获得图像,则可以通过信息词典获得原始图像

then save the image at album然后将图像保存在相册中

and get the PHAsset object.并获取 PHAsset 对象。

the code was shown as below:代码如下所示:

  /// Save image to album.
  static func saveImageToAlbum(image: UIImage, completion: ( (Bool, PHAsset?) -> Void )? ) {
  let status = PHPhotoLibrary.authorizationStatus()
  
  if status == .denied || status == .restricted {
      completion?(false, nil)
      return
  }
  
  var placeholderAsset: PHObjectPlaceholder? = nil
  PHPhotoLibrary.shared().performChanges({
      let newAssetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
      placeholderAsset = newAssetRequest.placeholderForCreatedAsset
  }) { (suc, error) in
      DispatchQueue.main.async {
          if suc {
              let asset = self.getAsset(from: placeholderAsset?.localIdentifier)
              completion?(suc, asset)
          } else {
              completion?(false, nil)
          }
      }
  }
}

  private static func getAsset(from localIdentifier: String?) -> PHAsset? {
  guard let id = localIdentifier else {
      return nil
  }
  let result = PHAsset.fetchAssets(withLocalIdentifiers: [id], options: nil)
  if result.count > 0{
      return result[0]
  }
  return nil

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM