从ios中的UIImageWriteToSavedPhotosAlbum获取图像名称[重复]
Posted
技术标签:
【中文标题】从ios中的UIImageWriteToSavedPhotosAlbum获取图像名称[重复]【英文标题】:Get image name from UIImageWriteToSavedPhotosAlbum in ios [duplicate] 【发布时间】:2013-12-23 10:53:25 【问题描述】:我已经搜索了如何获取从相机拍摄的已保存图像的名称,但我没有找到简单的东西。
这是我的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera)
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
NSLog(@"IMAGE SAVED TO PHOTO ALBUM");
[library assetForURL:assetURL resultBlock:^(ALAsset *asset )
NSLog(@"we have our ALAsset!");
NSLog(@"%@", assetURL);
failureBlock:^(NSError *error )
NSLog(@"Error loading asset");
];
];
你有答案,我很乐意测试它。
提前致谢。
【问题讨论】:
我已经尝试过他们的解决方案,但它们对我不起作用。 【参考方案1】:-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
NSURL *resourceURL;
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image =[[UIImage alloc] init];
image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
NSString *imageName = [imagePath lastPathComponent];
resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
NSData *imageData;
NSString *extensionOFImage =[imageName substringFromIndex:[imageName rangeOfString:@"."].location+1 ];
if ([extensionOFImage isEqualToString:@"jpg"])
imageData = UIImagePNGRepresentation(image);
else
imageData = UIImageJPEGRepresentation(image, 1.0);
int imageSize=imageData.length/1024;
NSLog(@"imageSize--->%d", imageSize);
if (imageName!=nil)
NSLog(@"imageName--->%@",imageName);
else
NSLog(@"no image name found");
如果您使用的是 ASSerts
-(void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
[self dismissViewControllerAnimated:YES completion:^
if (assets.count < 1) return;
//self.pageControl.numberOfPages = assets.count;
int index = 0;
for (ALAsset *asset in assets)
// NSString *imageName = [[asset defaultRepresentation] filename];
UIImage *image = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];
// (@"%@", [[asset defaultRepresentation] filename]);
NSData *imageData = UIImagePNGRepresentation(image);
int imageSize=imageData.length/1024;
(either)
NSURL *imagePath = [asset objectForKey:@"UIImagePickerControllerReferenceURL"];
// NSURL *imagePath = [NSURL URLWithString:[asset ob]];
NSString *imageName = [imagePath lastPathComponent];
(or)
NSLog(@"%@",[[asset defaultRepresentation] filename]);
index++;
];
【讨论】:
感谢您的帮助,我不使用资产。我试过你的解决方案(第一个),但照片没有保存:( 得到 imageData 后在控制台打印一次,看看打印了多少字节数据。 此语句后 image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];写 if(image) NSLog(@"Have image"); elseNSLog(@"no Image") if the statement is true 分享你显示图像的代码再次检查我的代码,我们会回复您 我添加了 nslog,它返回我有一个图像。 您能否分享一下您如何显示图像的代码。【参考方案2】:试试下面的代码。我在我以前的一个项目中使用过它,它对我有用:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
_selectedImage = info[UIImagePickerControllerEditedImage];
self.photoView.image = _selectedImage;
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSLog(@"[imageRep filename] : %@", [imageRep filename]);
;
// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
[picker dismissViewControllerAnimated:YES completion:NULL];
【讨论】:
感谢您的帮助,但文件名始终为空且照片未保存:(以上是关于从ios中的UIImageWriteToSavedPhotosAlbum获取图像名称[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何从 iOS 中的 Firebase 消息通知中获取主题?
从iOS中的json动态创建UITableView中的UILabels?
IOS 从 AppDelegate 中的 addMessageFromRemoteNotification 显示 UIViewController