我想这个名字的图像从库中挑选或新点击 -
-(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];
}
}
我怎样才能做到这一点?
使用代码波纹管,你会得到这样的路径: 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);
}];
看看在UIImagePickerControllerDelegate协议。 该字符串UIImagePickerControllerReferenceURL包含您的文件的URL。 你可以用它来构建它的文件名。
除此之外似乎没有内置的方式从一个的UIImage或UIImageView的对象获取的文件名。
参考: #1回答
您可以使用下面的代码获取文件名:
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给资源的原始文件名。