我想获取从图书馆挑选的图片的名称或新点击的图片 -
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
我正在使用此代码:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
AppDelegate *del = [[UIApplication sharedApplication]delegate];
[self dismissModalViewControllerAnimated:YES];
image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(@"imagekbgfg=====>%@",image);
[self.imageView setImage:image];
imgName = [info objectForKey:UIImagePickerControllerOriginalImage];
del.mainImgName = imgName;
del.imageData = UIImageJPEGRepresentation(image, 1.0);
del.imageApp = image;
}
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
}
我该怎么做?
答案 0 :(得分:6)
使用下面的代码,当您从相册中挑选图片时,您将获得类似assets-library://asset/asset.JPG?id=79450962-C0FD-484C-808B-83E045F40972&ext=JPG
的路径。此路径有效。它包含资产ID和资产类型。
// Firstly get the picked image's file URL.
NSURL *imageFileURL = [info objectForKey:UIImagePickerControllerReferenceURL];
// Then get the file name.
NSString *imageName = [imageFileURL lastPathComponent];
NSLog(@"image name is %@", imageName);
如果您希望使用UIImagePickerController
使用的路径再次选择图像用户。(这里可能您希望保存路径,以便您可以在不要求用户再次选择该图像的情况下读回该图像)您将需要ALAssetsLibrary
,否则您不需要它。
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block UIImage *returnValue = nil;
[library assetForURL:imageFileURL resultBlock:^(ALAsset *asset) {
returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
} failureBlock:^(NSError *error) {
NSLog(@"error : %@", error);
}];
答案 1 :(得分:0)
看看UIImagePickerControllerDelegate协议。字符串 UIImagePickerControllerReferenceURL 包含文件的URL。您可以使用它从中构造文件名。
除此之外,似乎没有内置方法从UIImage或UIImageView对象获取文件名。
答案 2 :(得分:0)
您可以使用以下代码获取文件名:
NSURL *assetURL = [imageDic objectForKey:UIImagePickerControllerReferenceURL];
__block NSString *originalFileName = nil;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset){
originalFileName = [[asset defaultRepresentation] filename];
}
originalFileName给出资源的原始文件名。