How to get Original Image and media type from PHAsset?

Mitesh Dobareeya picture Mitesh Dobareeya · Feb 8, 2016 · Viewed 12.7k times · Source

My **GMImagePickerController** returns the list of selected images from photos app.

The code are as follows.

  - (void)assetsPickerController:(GMImagePickerController *)picker  didFinishPickingAssets:(NSArray *)assetArray
     {
        NSLog(@"%@",assetArray);
        NSLog(@"GMImagePicker: User ended picking assets. Number of selected items is: %lu", (unsigned long)assetArray.count);
  }

The assetArray return result like this, I selected 3 images from photos app

 (
"<PHAsset: 0x7fa39e02e840> 1AEEF04A-F8AB-4019-AAB5- 2875CFD8F8E3/L0/001 mediaType=1/0, sourceType=1, (425x425), creationDate=2016-02-03 13:53:17 +0000, location=0, hidden=0, favorite=0 ",
"<PHAsset: 0x7fa39e02c840> 50489C13-55D0-4518-B290-B01B99D66996/L0/001 mediaType=1/0, sourceType=1, (425x335), creationDate=2016-02-03 13:53:08 +0000, location=0, hidden=0, favorite=0 ",
"<PHAsset: 0x7fa39e02c750> D0A466B2-9CF2-4FD9-A12F-07921A1D0E8F/L0/001 mediaType=1/0, sourceType=1, (425x365), creationDate=2016-02-03 13:53:04 +0000, location=0, hidden=0, favorite=0 "
 )

Now the problem is I want to get OriginalImage and mediaType from above result to store image into document directory. Please help me to solve this problem.

Answer

sahiljain picture sahiljain · Feb 25, 2017

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
 }