如何在iphone中获取通过照片库选取的图像名称?

San*_*ngh 24 iphone objective-c uiimagepickercontroller ios

我正在从iphone应用程序中的照片库中选择一个图像.我将如何检索实际图像名称.

在.h班

UIImageView * imageView;

UIButton * choosePhotoBtn;
Run Code Online (Sandbox Code Playgroud)

在.m级

-(IBAction) getPhoto:(id) sender 
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    if((UIButton *) sender == choosePhotoBtn)
    {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else 
    { 
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    [self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
Run Code Online (Sandbox Code Playgroud)

我如何获得图像的实际名称?

我是iphone的新手.请帮我.

提前致谢.

Joa*_*nne 68

导入文件中的AssetsLibrary:

#import <AssetsLibrary/AssetsLibrary.h>
Run Code Online (Sandbox Code Playgroud)

而且,在 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

// get the ref url
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];
Run Code Online (Sandbox Code Playgroud)

所以你将在日志中获得图像名称.

*不要忘记添加现有框架:AssetsLibrary.framework步骤:

  1. 在项目导航器中,选择您的项目
  2. 选择目标
  3. 选择"构建阶段"选项卡
  4. 打开'Link Binaries With Libraries'扩展器
  5. 单击"+"按钮
  6. 选择你的框架
  7. (可选)将添加的框架拖放到"框架"组

来源: http ://www.raywenderlich.com/forums/viewtopic.php?f = 2&p = 34901& 如何在Xcode 4中"添加现有框架"?

  • 在我看来,这是最好的答案. (2认同)

小智 9

如果您正在为iOS 9+目标构建,您将看到ALAssetsLibrary的一系列弃用警告,即:

在iOS 9.0中不推荐使用'assetForURL(_:resultBlock:failureBlock :)':使用fetchAssetsWithLocalIdentifiers:options:在PHAsset上通过本地标识符获取资产(或者通过以前已知的ALAssetPropertyAssetURL查找PHAssets,使用来自Photos框架的fetchAssetsWithALAssetURLs:options :)代替

如警告所述,您应该使用PHAsset.例如,使用swift 2.x,您需要先添加import Photos到文件中.然后,在didFinishPickingMediaWithInfoUIImagePickerControllerDelegate方法中使用fetchAssetsWithALAssetURLs获取文件名:

if let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
    let result = PHAsset.fetchAssetsWithALAssetURLs([imageURL], options: nil)
    let filename = result.firstObject?.filename ?? ""
}
Run Code Online (Sandbox Code Playgroud)

这将设置filename为"IMG_0007.JPG".


Bar*_*zyk 6

简单的Swift实现:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL {

        ALAssetsLibrary().assetForURL(referenceUrl, resultBlock: { asset in

            let fileName = asset.defaultRepresentation().filename()
            //do whatever with your file name

            }, failureBlock: nil)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

记住: import AssetsLibrary

  • XCode表示不推荐使用ALAssetsLibrary,而是使用PHPhotoLibrary (2认同)

小智 5

在Objective C中,使用Photos框架并导入Photos/Photos.h

然后,在imagePickerController函数中添加以下内容以从照片库中获取图像的文件名

NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
NSString *filename = [[result firstObject] filename];
Run Code Online (Sandbox Code Playgroud)