0

I need to check if an image is animated or ordinary image. Is there any method in iOS to check this?

I think a file with gif extension can be still image or animated image.

How can I check if it's animated or still image?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Rinto Andrews
  • 60
  • 1
  • 9

3 Answers3

4

For anyone that still comes here looking for an answer in Swift. Just to get if the image is an animated gif, this is the simplest I could come up to. This is working fine on Swift 4. For Swift 4.2 a couple changes were made to the delegate methods, but nothing major.

THIS ONLY WORKS ON IOS 11+, if you need support for 9/10 see below.

func isAnimatedImage(_ imageUrl: URL) -> Bool {
    if let data = try? Data(contentsOf: imageUrl),
          let source = CGImageSourceCreateWithData(imageData as CFData, nil) {

        let count = CGImageSourceGetCount(source)
        return count > 1
    }
    return false
}

and in my case I'm getting the image from a UIImagePickerController

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {

    if let imageURL = info[UIImagePickerControllerImageURL] as? URL {
        let isImageAnimated = isAnimatedImage(imageURL)
        print("isAnimated: \(isImageAnimated)")
    }

    picker.dismiss(animated: true, completion: nil)
}

IOS 9 and 10

For iOS 9 and 10 theres no way to get the UIImagePickerControllerImageURL since it's a new iOS 11 thing, so we need to use the Photo library to fetch the data from.

Import Photos Library at the top

import Photos

Add the correct code to fetch data

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {

    if let imageUrl = info[UIImagePickerControllerReferenceURL] as? URL {

        let asset = PHAsset.fetchAssets(withALAssetURLs: [imageUrl], options: nil)
        if let image = asset.firstObject {
            PHImageManager.default().requestImageData(for: image, options: nil) { (imageData, _, _, _) in
                let isImageAnimated = isAnimatedImage(imageData)
                print("isAnimated: \(isImageAnimated)")
            }
        }


    }

    picker.dismiss(animated: true, completion: nil)

}

func isAnimatedImage(_ imageData: Data) -> Bool {
    if let source = CGImageSourceCreateWithData(imageData as CFData, nil) {
        let count = CGImageSourceGetCount(source)
        return count > 1
    }
    return false
}
gmogames
  • 2,993
  • 1
  • 28
  • 40
1

the following code snippets maybe help you.

Basically, it uses CGImageSourceGetCount to get the count of images for gif files. Then, it depends on the count to do a image action or the animation action.

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

size_t count = CGImageSourceGetCount(source);

UIImage *animatedImage;

if (count <= 1) {
    animatedImage = [[UIImage alloc] initWithData:data];
}
else {
    NSMutableArray *images = [NSMutableArray array];

    NSTimeInterval duration = 0.0f;

    for (size_t i = 0; i < count; i++) {
        CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
   ....
    }
}

Those codes from [轉]用UIImage承載GIF圖片

Jim Tsai
  • 111
  • 7
-1

It is really possible but you may create your own method to do that by extending the NSData class.

here is the detail of getting an image extension: Detail

Putra
  • 399
  • 9
  • 23
  • a file with gif may be animated or not animated. right. am not looking for file extention. – Rinto Andrews Jan 17 '18 at 10:49
  • Well I answered based on your question. This was your question: "How to detect gif is Animate GIF in IOS As part of a project need to check the selected image is Animated gif or ordinary image. Is there any method in ios to check this. Thanks in advance" – Putra Jan 17 '18 at 12:42
  • You updated your question 47 minutes after I answered your initial question. – Putra Jan 17 '18 at 12:57