如何从PHAsset获取图像URL?是否可以使用PHAsset URL将图像保存到文档目录中?

Keyur

我用了

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

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

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

资产=在这里,您必须通过您的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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用writeToFile将图像保存在文档目录中?

将图像保存到“文档”目录并检索电子邮件附件

如何从PHAsset获取ALAsset URL?

如何使用Python PIL将图像保存到特定目录?

可以从PHAsset获取图像

如何将UIImage保存到文档目录?

在php中调整大小后如何将图像保存到目录?

使用python将多个图像保存到目录

如何将ByteData(图像)保存到Flutter中的Download目录中?

Python如何将图像保存到其他目录?

获取图像的PHAsset / localIdentifier保存到照片库

将图像保存到Google App脚本中的文档属性

如何通过URL将图像保存到目录中

将相机图像保存到目录中

照片框架-如何从PHAsset获取图像的CLLocation?

从PHAsset获取图像时如何枚举?(迅速)

将图像保存到Qt中动态创建的目录中

如何使用离子库将图像下载并保存到应用程序目录中

如何从url下载图像并将其保存到使用javascript的本地目录中?

如何将数组保存到ios文档目录?

使用Javascript将图像从url保存到硬盘

如何仅将最后拍摄的图像保存到目录?

尝试将图像保存到目录,保存到未知位置

将图像保存到文档目录后更新 bool 变量在 Swift 中无法按预期工作

如何从适用于 iOS 13 的 PHAsset 获取图像 URL

从 PHAsset 问题获取图像以检测图像更新

如何将图像从 url 保存到资产文件夹中

如何使用 url react native 将图像保存到变量

是否可以使用 Pycharm 将 Python 文件保存到图像中?