Xcode/ios:让用户select一张显示后如何获取图片名称?

Xcode/ios: How do you get name of image after letting user select one and displaying it?

我有代码可以让用户 select 现有图像并让它在屏幕上的 UIView 中很好地显示。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    NSLog(@"image is%@",chosenImage);
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

如何获取文件名以保存它以备将来参考? chosenImage 只是向您显示有关该文件的一堆元数据。

谢谢。

在该方法的开头尝试 NSLog(@"%@",info) 并查看该名称是否在字典中。

如下所示,您无法使用当前存在的 API 从 UIImagePickerControllerDelegate 中获取所选图像的名称。

这是可用的 UIImagePickerControllerDelegate Editing Information Keys:

NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;

但是,您可能希望使用 AssetsLibrary.framework 来获取照片应用程序管理的图像的名称:

@import AssetsLibrary;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:info[UIImagePickerControllerReferenceURL] 
             resultBlock:^(ALAsset *fileAsset) {

      NSLog(@"%@", [[fileAsset defaultRepresentation] filename]);

   } failureBlock:nil];
}