我已经实现了委托方法,可以访问所选的UIImage,如下所示:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *pickedImage = [info valueForKey:UIImagePickerControllerOriginalImage];
我想将其保存到目录中,但我需要一个文件名。因此,不仅仅是选择一个随机文件名,选择与图像相同的文件名是有意义的。
有没有办法从该信息字典中检索它?
答案 0 :(得分:3)
您可以使用信息词典的UIImagePickerControllerReferenceURL
值而不是UIImagePickerControllerOriginalImage
。这应该为您提供原始媒体项的URL。
这将返回一个NSURL
对象,然后您可以使用它来构建新的文件名。
答案 1 :(得分:0)
使用以下代码
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
{
[library assetForURL:assetURL resultBlock:^(ALAsset *asset )
{
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSLog(@"reference image filename picking from camera: %@", [imageRep filename]);
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:assetURL resultBlock:resultblock failureBlock:nil];
}
failureBlock:^(NSError *error )
{
NSLog(@"Error loading asset");
}];
}];
imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
imageView.contentMode = UIViewContentModeScaleAspectFit;