如何从PHAsset获取图像URL?是否可以使用PHAsset URL保存图像以记录目录?

时间:2017-06-10 10:09:25

标签: ios objective-c ipad nsdata phphotolibrary

我用过

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

但是当我尝试使用网址保存图片时,网址为零。

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

3 个答案:

答案 0 :(得分:5)

您可以从PHContentEditingInput获取imageURL:

夫特:

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

目标-C:

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

答案 1 :(得分:5)

asset =在这里你必须通过你的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];
}

检查横向模式或纵向模式的图像

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

将此权限添加到info.plist文件

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

答案 2 :(得分:0)

///这是使用Himanshu Moradiya的PHAsset答案获取图像URL的完整Swift代码

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