简体   繁体   English

如何从PHAsset获取原始图像和媒体类型?

[英]How to get Original Image and media type from PHAsset?

My **GMImagePickerController** returns the list of selected images from photos app. 我的**GMImagePickerController**从照片应用程序返回所选图像的列表。

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 像这样的assetArray返回结果,我从照片app中选择了3张图片

 (
"<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. 现在的问题是我想从上面的结果中获取OriginalImagemediaType以将图像存储到文档目录中。 Please help me to solve this problem. 请帮我解决这个问题。

To check media type you can use the following property of phasset 要检查媒体类型,您可以使用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: 要从PHAsset获取原始图像,您可以执行以下操作:

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. 使用PHImageManager,我们可以获得完整的原始图像。

You can get the URL to the original file and check the content. 您可以获取原始文件的URL并检查内容。 With this, you can get EXIF data and all that stuff: 有了这个,您可以获得EXIF数据和所有内容:

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

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

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