How to get Original Image and media type from PHAs

2020-03-24 09:14发布

问题:

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.

回答1:

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
 }


回答2:

 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.



回答3:

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)
      })
  }