List all gif files in the photo library

I have a requirement in which the user needs to get the gif from the list of gif files in the library. I tried to extract both images and videos without any problems. But when I used kUTTypeGIF as the media, it crashes with an error:

Application termination due to uncaught exception "NSInvalidArgumentException", reason: "There are no types available for source 0

Here is my code:

 #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 

How can i solve this? And if kUTTypeGIF media is not supported kUTTypeGIF , how can I show a list of all gif files to the user to select it? I need to display gif files only in UIImagePickerController

+8
ios objective-c uiimagepickercontroller
source share
3 answers

iOS doesn't give you a simple way to determine - when using the UIImagePickerController - which basic file format is for images stored in the camera frame. Apple's philosophy here is that the image should be considered as a UIImage object and that it doesn’t matter to you which file format it is.

So, since you cannot use the UIImagePickerController to filter GIF files. Here are a couple of possibilities for you:

one)

Once you select an image, you can determine which file it is. Here is an example question that asks how to determine if an image is a PNG or JPEG . As soon as the user selects the file, you will find out if it is GIF or JPEG or PNG or something else.

2)

You can convert any UIImage to a GIF file. This raises a question that points to a library that might help .

3)

You can iterate over the entire frame of the camera and convert / save these images to the application document directory as GIF files. Something that starts with the listing found in this related question , and then launches each image through the ImageIO environment to convert it to a gif file (the code for which I indicated in solution No. 2). Then you can flip your own collector.

ps your own code will not work because, as Nathan noted, GIF not a type of multimedia. This is a function that indicates the available media types:

 -(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]; } 
+3
source share

If you only want to extract resources from the photo library without selection, you can use PHFetchResult to get an array of PHAsset . Below is a list of the available MediaType enums available in Photos.Framework :

 typedef NS_ENUM(NSInteger, PHAssetMediaType) { PHAssetMediaTypeUnknown = 0, PHAssetMediaTypeImage = 1, PHAssetMediaTypeVideo = 2, PHAssetMediaTypeAudio = 3, } PHOTOS_ENUM_AVAILABLE_IOS_TVOS(8_0, 10_0); 

You can use it like:

 PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil]; 

and request an image from the asset as:

 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); }]; 

Hope this helps you!

+3
source share

In iOS 11, you can get all gifs using Smart Album.

 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>() } 
+1
source share

All Articles