我正在从iphone应用程序中的照片库中选择一张图片。我将如何检索实际的图像名称。
in .h class
UIImageView * imageView;
UIButton * choosePhotoBtn;
<。>在.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"];
}
我如何获得图像的实际名称?
我是iphone的新手。请帮帮我。
提前致谢。
答案 0 :(得分:67)
导入文件中的AssetsLibrary:
#import <AssetsLibrary/AssetsLibrary.h>
而且,在- (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];
因此您将在日志中获取图像名称。
*不要忘记添加现有框架:AssetsLibrary.framework 步骤进行:
来源: http://www.raywenderlich.com/forums/viewtopic.php?f=2&p=34901 &安培; How to "add existing frameworks" in Xcode 4?
答案 1 :(得分:8)
如果您正在构建适用于iOS 9+的目标,您将看到一堆ALAssetsLibrary的弃用警告,即:
&#39; assetForURL(_:resultBlock:failureBlock:)&#39;在iOS 9.0中已弃用:使用fetchAssetsWithLocalIdentifiers:options:在PHAsset上通过本地标识符获取资产(或者通过以前已知的ALAssetPropertyAssetURL查找PHAssets,使用来自Photos框架的fetchAssetsWithALAssetURLs:options :)
如警告所述,您应该使用PHAsset。例如,使用swift 2.x,您需要先将import Photos
添加到文件中。然后,在didFinishPickingMediaWithInfo
UIImagePickerControllerDelegate方法中使用fetchAssetsWithALAssetURLs
来获取文件名:
if let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
let result = PHAsset.fetchAssetsWithALAssetURLs([imageURL], options: nil)
let filename = result.firstObject?.filename ?? ""
}
这会将filename
设置为&#34; IMG_0007.JPG&#34;。
答案 2 :(得分:5)
简单的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)
}
}
}
请记住:import AssetsLibrary
答案 3 :(得分: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];
答案 4 :(得分:2)
虽然您可以检索最后一个路径组件并将其用作文件名,但建议不要这样做。这些文件名由系统分配,供iTunes在同步时理解,不适合程序员访问,因为在将来的同步中它们可能被其他一些图像替换。
关于此问题的一个好方法是将当前日期指定为文件名,同时保存为从图库中挑选的图像。您可以将其保存在文档或库目录中,并使用映射PList文件将图像映射到其文件名。
或者,您也可以将唯一编号指定为文件名,并使用这些值访问图像。
答案 5 :(得分:0)
从 Swift 3和iOS8 + 开始,.filename不再可访问。它仍可通过self.valueForKey(“filename”)获得,但不太合法。
但是,我在问题this answer中发现"iOS8 Photos Framework: How to get the name(or filename) of a PHAsset?"简短,合法。
答案 6 :(得分:0)
Objective C 实施。似乎不推荐使用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];
}
答案 7 :(得分:0)
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);
}
答案 8 :(得分:0)
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)