Ios 列出照片库中的所有gif文件
我有一个要求,用户需要从库中的gif文件列表中获取gif。我尝试获取图像和视频,没有任何问题。但当我使用Ios 列出照片库中的所有gif文件,ios,objective-c,uiimagepickercontroller,Ios,Objective C,Uiimagepickercontroller,我有一个要求,用户需要从库中的gif文件列表中获取gif。我尝试获取图像和视频,没有任何问题。但当我使用kuttypegf作为媒体时,它会因错误而崩溃: 由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“没有源的可用类型。” 0' 这是我的密码: #import "ViewController.h" #import <MobileCoreServices/MobileCoreServices.h> @interface ViewCont
kuttypegf
作为媒体时,它会因错误而崩溃:
由于未捕获异常而终止应用程序
“NSInvalidArgumentException”,原因:“没有源的可用类型。”
0'
这是我的密码:
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(IBAction)btnChooseGif:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeGIF, nil]; // Here is the crash
[self presentViewController:imagePicker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
}
@end
#导入“ViewController.h”
#进口
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
}
-(iAction)btnChooseGif:(id)发送方{
UIImagePickerController*imagePicker=[[UIImagePickerController alloc]init];
imagePicker.delegate=self;
imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes=[[NSArray alloc]initWithObjects:(NSString*)Kuttypegf,nil];//下面是崩溃
[self-presentViewController:imagePicker动画:是完成:无];
}
-(void)imagePickerController:(UIImagePickerController*)picker未使用信息完成PickingMediaWithInfo:(NSDictionary*)信息
{
}
@结束
我怎样才能解决这个问题?如果这里不支持
kuttypegf
media,我如何向用户显示所有gif文件的列表以供选择?我只需要在UIImagePickerController中显示gif文件iOS无法让您在使用UIImagePickerController时轻松确定存储在相机卷中的图片的基本文件格式。苹果公司的理念是,图像应该被视为UIImage
对象,您不应该关心最终的文件格式是什么
因此,由于无法使用UIImagePickerController过滤GIF文件。这里有几个可能性:
(一)
一旦你选择了一个图像,你就可以确定它是什么类型的文件。一旦用户选择了一个文件,您就知道它是GIF还是JPEG或PNG或其他文件
(二)
您可以将任何UIImage转换为GIF
文件
(三)
您可以在整个相机卷上进行迭代,并将这些图像转换/保存到应用程序的文档目录中,作为GIF
文件。它启动并通过ImageIO框架运行每张图片,将其转换为gif文件(我在解决方案2中指出的代码)。然后,您可以滚动自己的选择器
p、 正如Nathan指出的那样,gif
不是一种媒体类型,所以你自己的代码不起作用。这是一个指出可用介质类型的函数:
-(IBAction)btnChooseGif:(id)sender {
NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
NSLog(@"availableMedia is %@", availableMedia);
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
[self presentViewController:imagePicker animated:YES completion:nil];
}
如果您只想从照片库中提取资产而不使用选择器,则可以使用
PHFetchResult
获取PHAsset
的数组。下面是可用的MediaType枚举列表,可在照片中找到。Framework
:
typedef NS_ENUM(NSInteger, PHAssetMediaType) {
PHAssetMediaTypeUnknown = 0,
PHAssetMediaTypeImage = 1,
PHAssetMediaTypeVideo = 2,
PHAssetMediaTypeAudio = 3,
} PHOTOS_ENUM_AVAILABLE_IOS_TVOS(8_0, 10_0);
您可以将其用作:
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
并从资产中请求图像作为:
PHImageManager *manager = [PHImageManager defaultManager];
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
requestOptions.synchronous = true;
[manager requestImageDataForAsset:asset options:requestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
NSLog(@"Data UTI :%@ \t Info :%@",dataUTI,info);
}];
希望这对你有帮助 在iOS 11中,您可以通过智能相册获取所有gif
func getGif() -> PHFetchResult<PHAsset> {
if let gifCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumAnimated, options: nil).firstObject {
return PHAsset.fetchAssets(in: gifCollection, options: nil)
}
return PHFetchResult<PHAsset>()
}
func getGif()->PHFetchResult{
如果let gifCollection=PHAssetCollection.fetchAssetCollections(带:.smartAlbum,子类型:.smartAlbumAnimated,选项:nil)。firstObject{
返回PHAsset.fetchAssets(in:gifCollection,options:nil)
}
返回PHFetchResult()
}
检查这个@DSDharma,这不是我的问题。我希望我的应用程序列出图像选择器本身中的所有gif文件。如果选择gif,则用户在手机中只能看到gif文件gif不是有效的媒体类型。允许的媒体类型为图像
和电影
。您可以使用UIImagePickerController检查接受的类型。可用的中间类型(用于:.photoLibrary)
此外,从iOS 11开始,动画图像被分组到名为“animated
”的智能相册中。所以您可以使用PHAssetCollection来查找它。