How To get Image URL From PHAsset? Is it possible To save Image using PHAsset URL to document Directory?

荒凉一梦 提交于 2019-12-01 07:28:58

问题


I used

  `NSURL *urlA = [info valueForKey:@"PHImageFileURLKey"];`

but when i try to save image using URL then URL is nil.

 `NSData *pngData =[NSData dataWithContentsOfURL:urlA options:NSDataReadingMapped error:nil];`

回答1:


asset = Here you have to pass your PHAsset .

PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
 [[PHImageManager defaultManager]
             requestImageDataForAsset:asset
                            options:imageRequestOptions
                      resultHandler:^(NSData *imageData, NSString *dataUTI,
                                      UIImageOrientation orientation, 
                                      NSDictionary *info) 
     {
          NSLog(@"info = %@", info);
          if ([info objectForKey:@"PHImageFileURLKey"]) {

               NSURL *path = [info objectForKey:@"PHImageFileURLKey"];
               // if you want to save image in document see this.
               [self saveimageindocument:imageData withimagename:[NSString stringWithFormat:@"DEMO"]];
          }                                            
    }];

-(void) saveimageindocument:(NSData*) imageData withimagename:(NSString*)imagename{

    NSString *writePath = [NSString stringWithFormat:@"%@/%@.png",[Utility getDocumentDirectory],imagename];

    if (![imageData writeToFile:writePath atomically:YES]) {
        // failure
        NSLog(@"image save failed to path %@", writePath);

    } else {
        // success.
        NSLog(@"image save Successfully to path %@", writePath);
    }

}
+ (NSString*)getDocumentDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

Check image that landscape mode or portrait mode

if (chooseimage.size.height >= chooseimage.size.width)
{
         Islandscape = NO;
}else{
     UIImage* landscapeImage = [UIImage imageWithCGImage:chooseimage.CGImage
                                                      scale:chooseimage.scale
                                                orientation:UIImageOrientationLeft];
        self.imgPreviewView.image = landscapeImage;
        self.imgPreviewView.contentMode = UIViewContentModeScaleAspectFill;
        Islandscape = YES;
 }

Add this permission into info.plist file

<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your photo library to let you select a picture.</string>



回答2:


You can get the imageURL from PHContentEditingInput:

Swift:

asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (eidtingInput, info) in
  if let input = eidtingInput, let imgURL = input.fullSizeImageURL {
     // imgURL 
  }
}

Objective-C:

[asset requestContentEditingInputWithOptions:[PHContentEditingInputRequestOptions new] completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
    NSURL *imageURL = contentEditingInput.fullSizeImageURL;
}];



回答3:


//This is the Complete Swift Code of getting image URL with PHAsset answer of Himanshu Moradiya

  let imageRequestOptions = PHImageRequestOptions()
  PHImageManager.default().requestImageData(for: currentAsset, options: imageRequestOptions, resultHandler: { imageData, dataUTI, orientation, info in
    if let info = info {
      print("info = \(info)")
    }
    if info?["PHImageFileURLKey"] != nil {

      let path = info?["PHImageFileURLKey"] as? URL
       print(path) //here you get complete URL


      // if you want to save image in document see this.
     // self.saveimageindocument(imageData, withimagename: "DEMO")
    }
  })


来源:https://stackoverflow.com/questions/44472070/how-to-get-image-url-from-phasset-is-it-possible-to-save-image-using-phasset-ur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!