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步骤:
来源: http ://www.raywenderlich.com/forums/viewtopic.php?f = 2&p = 34901& 如何在Xcode 4中"添加现有框架"?
小智 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".
简单的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
小智 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)
| 归档时间: |
|
| 查看次数: |
47757 次 |
| 最近记录: |