Earlier i used SDWebimage third party to display images, now i removed that 3rd party due some other reason. is there any other way to Display GIF image from URL in Imageview without using third party...
Asked
Active
Viewed 960 times
-3
-
Download it with NSURLSession and display it? – matt Feb 24 '17 at 10:38
-
@Vijayender Vaddepally search in google because you got multiple answer – Jigar Feb 24 '17 at 10:40
-
Any possibility with AFNetworking? – Vijayender Vaddepally Feb 24 '17 at 10:41
-
i searched but i didn't get correct answer ... – Vijayender Vaddepally Feb 24 '17 at 10:42
-
GIF is not supported by `UIImagewView` directly. So you will either build your own GIF parse or use a thirdparty library, – rckoenes Feb 24 '17 at 10:43
-
That why i posted this question how can you give -vote ? – Vijayender Vaddepally Feb 24 '17 at 10:47
-
Because there are tons of answers of your question, search properly before asking a question. – iPeter Feb 24 '17 at 10:49
-
I know Tons of answers available but that answers are older one. who knows some body founded alternative answer for that . – Vijayender Vaddepally Feb 24 '17 at 10:52
2 Answers
0
Well you can do it simply by following next steps:
Add the following
functions
#import <ImageIO/ImageIO.h> #import <MobileCoreServices/MobileCoreServices.h> UIImage *GIFFromData(NSData *data) { if (!data) { return nil; } CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)(data), NULL); UIImage *image = nil; if (GIFSourceContainsAnimatedGIF(source)) { image = GIFFromImageSource(source); } else { image = [UIImage imageWithData:data]; } if (source) { CFRelease(source); } return image; } BOOL GIFSourceContainsAnimatedGIF(CGImageSourceRef source) { return (source && UTTypeConformsTo(CGImageSourceGetType(source), kUTTypeGIF) && CGImageSourceGetCount(source) > 1); } UIImage *GIFFromImageSource(CGImageSourceRef source) { CFRetain(source); NSUInteger numberOfFrames = CGImageSourceGetCount(source); NSMutableArray<UIImage *> *images = [NSMutableArray arrayWithCapacity:numberOfFrames]; NSTimeInterval duration = 0.0; for (NSUInteger i = 0; i < numberOfFrames; ++i) { CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); if (image) { UIImage *frameImage = [UIImage imageWithCGImage:image scale:1.0 orientation:UIImageOrientationUp]; [images addObject:frameImage]; CFRelease(image); } else { continue; } duration += GIFSourceGetFrameDelay(source, i); } CFRelease(source); return [UIImage animatedImageWithImages:images duration:duration]; } NSTimeInterval GIFSourceGetFrameDelay(CGImageSourceRef source, NSUInteger index) { NSTimeInterval frameDelay = 0; CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(source, index, NULL); if (!imageProperties) { return frameDelay; } CFDictionaryRef gifProperties = nil; if (CFDictionaryGetValueIfPresent(imageProperties, kCGImagePropertyGIFDictionary, (const void **)&gifProperties)) { const void *durationValue = nil; if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFUnclampedDelayTime, &durationValue)) { frameDelay = [(__bridge NSNumber *)durationValue doubleValue]; if (frameDelay <= 0) { if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFDelayTime, &durationValue)) { frameDelay = [(__bridge NSNumber *)durationValue doubleValue]; } } } } CFRelease(imageProperties); return frameDelay; }
Download your GIF with NSURLSession
Display it
UIImage *image = GIFFromData(data); UIImageView *view = [[UIImageView alloc] initWithImage:image];
Hope this helps :)

Yassir
- 80
- 5
0
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.gif"],
[UIImage imageNamed:@"image2.gif"],
[UIImage imageNamed:@"image3.gif"],
[UIImage imageNamed:@"image4.gif"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];

Community
- 1
- 1

Chetan Hedamba
- 229
- 2
- 7