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?
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?
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
}
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圖片
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