Ios 如何从PHAsset获取图像URL?是否可以使用Phaset URL将图像保存到文档目录?
我曾经 但当我尝试使用URL保存图像时,URL为零Ios 如何从PHAsset获取图像URL?是否可以使用Phaset URL将图像保存到文档目录?,ios,objective-c,ipad,nsdata,phphotolibrary,Ios,Objective C,Ipad,Nsdata,Phphotolibrary,我曾经 但当我尝试使用URL保存图像时,URL为零 `NSURL *urlA = [info valueForKey:@"PHImageFileURLKey"];` 您可以从以下位置获取imageURL: 斯威夫特: `NSData *pngData =[NSData dataWithContentsOfURL:urlA options:NSDataReadingMapped error:nil];` 目标C: asset.requestContentEditingInput(with
`NSURL *urlA = [info valueForKey:@"PHImageFileURLKey"];`
您可以从以下位置获取imageURL: 斯威夫特:
`NSData *pngData =[NSData dataWithContentsOfURL:urlA options:NSDataReadingMapped error:nil];`
目标C:
asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (eidtingInput, info) in
if let input = eidtingInput, let imgURL = input.fullSizeImageURL {
// imgURL
}
}
asset=在这里,您必须通过PHAsset
[asset requestContentEditingInputWithOptions:[PHContentEditingInputRequestOptions new] completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *imageURL = contentEditingInput.fullSizeImageURL;
}];
选择横向模式或纵向模式的图像
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];
}
将此权限添加到info.plist文件中
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;
}
NSPhotoLibraryUsageDescription
$(产品名称)希望访问您的照片库,以便您选择图片。
//这是获取图像URL的完整Swift代码,带有Himanshu Moradiya的PHAsset答案
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your photo library to let you select a picture.</string>
@a-tuo请用客观C回答,因为我对这个问题一无所知Swift@KeyurAspiration您是否在plist中添加了“隐私-照片库使用说明”密钥以获得访问照片的权限?@a-tuo是的,我已经给出了此“隐私-照片库使用说明”不适用于我(iOS 14.4)PHImageFileURLKey未包含在字典中。如果映像不是本地映像,“isNetworkAccessAllowed”的属性需要设置为true。请参阅完整代码:
let requestOptions=PHContentEditingInputRequestOptions()requestOptions.isNetworkAccessAllowed=true asset.requestContentEditingInput(with:requestOptions){(editingInput,info)in}
检查我的答案希望您清楚回答谢谢ans我尝试了您的ans,但我在日志上收到了此类错误打印::设置安全信息:不允许操作,并且我尝试将图像从Phaset URL保存到文档目录,但是由于安全原因,我得到了一个错误-不允许访问。您必须在info.plist文件中添加我在回答中添加的一个内容@KeyurAspirationi已经在info.plist中添加了照片、摄像头、麦克风描述访问、,还有一件事是,图像数组以另一种方式保存到文档目录中,但没有内存不足的警告问题。因此,请告诉我。@KeyurAspiration检查我的更新答案,将图像存储在文档文件夹中。图像不是从服务器获取的,它是使用图像选择器库从照片库中拾取的。
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")
}
})