How to get Original Image and media type from PHAsset?

只愿长相守 提交于 2019-12-01 09:17:09

To check media type you can use the following property of phasset

if asset.mediaType == .image{
 //do anything for image asset

}else if asset.mediaType == .video{
 //do anything for video asset

}else if asset.mediaType == .audio{
 //do anything for audio asset
}

To get the original image from PHAsset you can do the following:

let requestImageOption = PHImageRequestOptions()
requestImageOption.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat

let manager = PHImageManager.default()
manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode:PHImageContentMode.default, options: requestImageOption) { (image:UIImage?, _) in
        // process the original image
 }
 NSLog(@"====%@====",assetArray);

for(int i=0;i<assetArray.count;i++)
{
    self.requestOptions = [[PHImageRequestOptions alloc] init];
    self.requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
    self.requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

    // this one is key
    self.requestOptions.synchronous = true;

    //  self.assets = [NSMutableArray arrayWithArray:assets];
    PHImageManager *manager = [PHImageManager defaultManager];
    Albumimages = [NSMutableArray arrayWithCapacity:[assetArray count]];

    // assets contains PHAsset objects.
    __block UIImage *ima;

    for (PHAsset *asset in assetArray) {
        // Do something with the asset

        [manager requestImageForAsset:asset
                           targetSize:PHImageManagerMaximumSize
                          contentMode:PHImageContentModeDefault
                              options:self.requestOptions
                        resultHandler:^void(UIImage *image, NSDictionary *info) {
                         //retrive all  images   
                     ima = image;


                        }];
    }

}

Using PHImageManager we can get full original image.

You can get the URL to the original file and check the content. With this, you can get EXIF data and all that stuff:

  /* Get the URL of the source image in the Asset */
  func getAssetURL(asset: PHAsset, completion: @escaping (_ url: URL?) -> Void) {
      let options = PHContentEditingInputRequestOptions()
      options.isNetworkAccessAllowed = false
      asset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, dictionary) in
          completion(contentEditingInput?.fullSizeImageURL)
      })
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!