如何从PHAsset获取图像URL?是否可以使用PHAsset URL将图像保存到文档目录? [英] How To get Image URL From PHAsset? Is it possible To save Image using PHAsset URL to document Directory?

查看:5516
本文介绍了如何从PHAsset获取图像URL?是否可以使用PHAsset URL将图像保存到文档目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过

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

但是当我尝试使用URL保存图像时,则URL为零。

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

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


推荐答案

asset =在这里你必须通过你的PHAsset。

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

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

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>

这篇关于如何从PHAsset获取图像URL?是否可以使用PHAsset URL将图像保存到文档目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆