[英]How can i get the name of image picked through photo library in iphone?
I am picking an image from photo library in iphone application. 我正在从iPhone应用程序的照片库中选择图像。 How will i retrieve the actual image name.
我将如何检索实际的图像名称。
in .h class 在.h类中
UIImageView * imageView;
UIButton * choosePhotoBtn;
in .m class 在.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"];
}
How will i get the actual name of image ? 我如何获得图像的实际名称?
I m new in iphone. 我是iPhone新手。 Please help me.
请帮我。
Thanks in advance. 提前致谢。
import AssetsLibrary in your file: 在文件中导入AssetsLibrary:
#import <AssetsLibrary/AssetsLibrary.h>
And, in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
并且,在
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
put 放
// 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];
so you'll get the image name in log. 这样您就可以在日志中获取图片名称。
*don't forget to add existing framework: AssetsLibrary.framework Steps: *不要忘记添加现有框架:AssetsLibrary.framework步骤:
Source: http://www.raywenderlich.com/forums/viewtopic.php?f=2&p=34901 & How to "add existing frameworks" in Xcode 4? 来源: http : //www.raywenderlich.com/forums/viewtopic.php?f=2&p=34901 & 如何在Xcode 4中“添加现有框架”?
If you are building for iOS 9+ target, you will see a bunch of deprecation warnings with ALAssetsLibrary, ie: 如果您是针对iOS 9+目标构建的,则将看到带有ALAssetsLibrary的一堆弃用警告,即:
'assetForURL(_:resultBlock:failureBlock:)' was deprecated in iOS 9.0: Use fetchAssetsWithLocalIdentifiers:options: on PHAsset to fetch assets by local identifier (or to lookup PHAssets by a previously known ALAssetPropertyAssetURL use fetchAssetsWithALAssetURLs:options:) from the Photos framework instead
在iOS 9.0中不推荐使用'assetForURL(_:resultBlock:failureBlock :)':在PHAsset上使用fetchAssetsWithLocalIdentifiers:options:以本地标识符获取资产(或通过先前已知的ALAssetPropertyAssetURL查找PHAssets,从框架中使用fetchAssetsWithALAssetURLs从照片:options):代替
As the warning describes, you should use PHAsset. 如警告所述,您应该使用PHAsset。 Using swift 2.x, for example, you will need to add
import Photos
to your file first. 例如,使用swift 2.x,您需要首先将
import Photos
添加到文件中。 Then, in the didFinishPickingMediaWithInfo
UIImagePickerControllerDelegate method use fetchAssetsWithALAssetURLs
to get the filename: 然后,在
didFinishPickingMediaWithInfo
UIImagePickerControllerDelegate方法中,使用fetchAssetsWithALAssetURLs
来获取文件名:
if let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
let result = PHAsset.fetchAssetsWithALAssetURLs([imageURL], options: nil)
let filename = result.firstObject?.filename ?? ""
}
This will set filename
to be something like, "IMG_0007.JPG". 这会将
filename
设置为“ IMG_0007.JPG”。
Simple Swift implementation: 简单的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)
}
}
}
Remember about: import AssetsLibrary
记住有关:
import AssetsLibrary
In Objective C, use the Photos framework and import Photos/Photos.h 在Objective C中,使用“照片”框架并导入Photos / Photos.h
Then, in your imagePickerController function add the following to get the filename of the image from the photo library 然后,在您的imagePickerController函数中添加以下内容以从照片库中获取图像的文件名
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
NSString *filename = [[result firstObject] filename];
Though you may be able to retrieve the last path component and use it like a file name, it is not advisable to do so. 尽管您可以检索最后一个路径组件并将其用作文件名,但建议不要这样做。 These filenames are assigned by the system for iTunes to understand while syncing and are not meant for programmers to access as they could be replaced by some other images in future syncs.
这些文件名是由系统分配给iTunes的,以便在同步时理解它们,而并非供程序员访问,因为它们可能在以后的同步中被其他图像替换。
A good round about for this is to assign the current Date as filenames, while saving to images picked from the gallery. 一个很好的解决方案是将当前日期指定为文件名,同时保存到从图库中选取的图像。 You may save it in your documents or library directory and use a mapping PList file to map images to their filename.
您可以将其保存在文档或库目录中,并使用映射PList文件将图像映射到其文件名。
Alternatively, you can also assign unique numbers as filenames and access the images using these values. 或者,您也可以分配唯一的数字作为文件名,并使用这些值访问图像。
Objective C implementation that works on iOS 10 . 适用于iOS 10的 Objective C实现。 ALAssetsLibrary seems to be deprecated so you should use PHAsset:
ALAssetsLibrary似乎已被弃用,因此您应该使用PHAsset:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
PHAsset *phAsset = [[PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil] lastObject];
NSString *imageName = [phAsset valueForKey:@"filename"];
UIImage *photo = [info valueForKey:UIImagePickerControllerOriginalImage];
NSLog(@"Picked image: %@ width: %f x height: %f",imageName, photo.size.width, photo.size.height);
[picker dismissViewControllerAnimated:YES completion:nil];
}
As of Swift 3 and iOS8+ the .filename is not accessible any more. 从Swift 3和iOS8 +开始 ,.filename不再可用。 It is still available through self.valueForKey("filename"), but not quite legal though.
它仍然可以通过self.valueForKey(“ filename”)获得,但不是很合法。
However, I found this answer in the question "iOS8 Photos Framework: How to get the name(or filename) of a PHAsset?" 但是,我在“ iOS8照片框架:如何获取PHAsset的名称(或文件名)”问题中找到了这个答案 。 to be short, simple, and legal.
简短,简单且合法。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageName!)
let image = info[UIImagePickerControllerOriginalImage]as! UIImage
let data = UIImagePNGRepresentation(image)
do
{
try data?.write(to: localPath!, options: Data.WritingOptions.atomic)
}
catch
{
// Catch exception here and act accordingly
}
self.dismiss(animated: true, completion: nil);
}
var fileName: String = "Attachment"
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let assets = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
let assetResource = PHAssetResource.assetResources(for: assets.firstObject!)
fileName = assetResource.first?.originalFilename ?? "Attachment"
}
print(fileName)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.