获取图片名称,网上大多数就一种获取相册图片名称,要么就是方法太旧。最近有这个需求,相册和拍照图片名称都要获取。
**** 注意一下,获取相册图片名称,用imagePickerController在高于iOS14上的系统获取可能会出现获取不到的问题,这个类已经老了。可以使用PHPicker
一、要求
1、获取相册图片名称
2、获取拍照图片名称
二、实现
本次记录的方法是调用系统相册、相机后,获取图片名称的实现
相册、相机调用就不写上去了。直接上获取名称的代码
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImage *image = [[info objectForKey:UIImagePickerControllerOriginalImage] fixOrientation];
//相机拍摄获取
if(@available(iOS9.0, *)) {
//唯一标识,可以用于图片资源获取
__blockNSString*createdAssetID =nil;
XZWeakSelf(weakSelf);
[[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
PHAssetChangeRequest *changeAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
createdAssetID = changeAssetRequest.placeholderForCreatedAsset.localIdentifier;
}completionHandler:^(BOOLsuccess,NSError*_Nullableerror) {
//获取图片内容信息必须要放在completionHandler后。不然可能拿不到
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[createdAssetID] options:nil];
PHAsset*asset = fetchResult.firstObject;
NSString*filename = [assetvalueForKey:@"filename"];
if(![NSStringstrNilOrEmpty:filename]) {
//判空
[weakSelfp_cameraWithInfo:infofileName:filename];
}else{
//失败
}
}];
}else{
XZWeakSelf(weakSelf);
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[libwriteImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
if(error) {
NSLog(@"error:%@", error);
}else{
PHFetchResult*fetchResult = [PHAssetfetchAssetsWithALAssetURLs:@[assetURL]options:nil];
PHAsset*asset = fetchResult.firstObject;
NSString*filename = [assetvalueForKey:@"filename"];
if(![NSStringstrNilOrEmpty:filename]) {
[weakSelfp_cameraWithInfo:infofileName:filename];
}
}
}];
}
}else{
//相册获取
[self p_photoLibraryWithInfo:info];
}
}
//相册选择
- (void)p_photoLibraryWithInfo:(NSDictionary
NSString*imageName =@"";
NSString*imageType =@"";
//给前端压缩一下图片Jpeg格式
NSURL *imageUrl = [info valueForKey:UIImagePickerControllerReferenceURL];
if(imageUrl) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[imageUrl] options:nil];
PHAsset*asset = fetchResult.firstObject;
NSString*filename = [assetvalueForKey:@"filename"];
if(![NSStringstrNilOrEmpty:filename]) {
UIImage *image = [[info objectForKey:UIImagePickerControllerOriginalImage] fixOrientation];
imageName = [filenamestringByDeletingPathExtension];
imageType = [filenamepathExtension];
}else{
//失败
}
}
}
//相机选择
- (void)p_cameraWithInfo:(NSDictionary *)infofileName:(NSString*)fileName {
NSString *imageName = [fileName stringByDeletingPathExtension];//名称
NSString*imageType = [fileName pathExtension];//类型
UIImage *image = [[info objectForKey:UIImagePickerControllerOriginalImage] fixOrientation];
}
更新补充于2022-7-23
下面补充的是使用系统推荐使用的PHPicker获取图片名称的方法,iOS14以上由于图片类型增加,有些图片信息获取不了,所以用PHPicker获取。PHPicker初始化方法自行百度下就行,不需要特殊设置。值得一提的是,我这里获取图片类型是从NSItemProvider的description里面截取出来的。暂时没时间研究,测试后发现这样取也可以,暂时没发现问题,但是应该有更准确的方法获取才对。后续有时间再看吧。
#pragma mark- PHPickerViewControllerDelegate
- (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray
API_AVAILABLE(ios(14)){
[pickerdismissViewControllerAnimated:YES completion:nil];
if(results && results.count>0) {
PHPickerResult*result = results.firstObject;
NSItemProvider*provider = result.itemProvider;
if([providercanLoadObjectOfClass:[UIImageclass]]) {
[providerloadObjectOfClass:[UIImageclass]completionHandler:^(__kindofid _Nullableobject,NSError*_Nullableerror) {
//图片
UIImage*img = (UIImage*)object;
//图片名称
NSString*imageName = provider.suggestedName;
NSArray*typeArr = [provider.descriptioncomponentsSeparatedByString:@"."];
NSArray*typeArr1 = [[typeArrlastObject]componentsSeparatedByString:@"\n"];
//图片类型获取
NSString*imageType = [[typeArr1firstObject]uppercaseString];
}];
}
}
}