-3

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...

2 Answers2

0

Well you can do it simply by following next steps:

  1. 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;
    }    
    
  2. Download your GIF with NSURLSession

  3. 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];

Add animated Gif image in Iphone UIImageView

Community
  • 1
  • 1
Chetan Hedamba
  • 229
  • 2
  • 7