我用了
`NSURL *urlA = [info valueForKey:@"PHImageFileURLKey"];`
但是,当我尝试使用URL保存图像时,URL为零。
`NSData *pngData =[NSData dataWithContentsOfURL:urlA options:NSDataReadingMapped error:nil];`
资产=在这里,您必须通过您的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] 删除。
我来说两句