所选图像xcode的名称

我想获得从图书馆采摘的图像的名称或新点击 –

-(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]; } } 

我怎样才能做到这一点?

使用下面的代码,你会得到如下的path: assets-library://asset/asset.JPG?id=79450962-C0FD-484C-808B-83E045F40972&ext=JPG当你从相册中select图片。 这条路是有效的。 它包含资产ID和资产types。

 // 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的path重新获取图像用户(这里也许你想保存path,以便你可以读取图像而不要求用户再次select该图像),否则你将需要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协议。 stringUIImagePickerControllerReferenceURL包含您的文件的URL。 你可以用它来构造它的文件名。

除此之外,似乎没有内置的方式从UIImage或UIImageView对象获取文件名。

参考: Stackoverflow的答案

您可以使用以下代码获取文件名称:

 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给出资源的原始文件名。