如何在Imageview中显示URL中的GIF图像?

时间:2017-02-24 10:35:50

标签: ios objective-c

早些时候我使用SDWebimage第三方显示图像,现在由于其他原因我删除了第三方。有没有其他方法可以在Imageview中显示来自URL的GIF图像,而无需使用第三方...

2 个答案:

答案 0 :(得分:0)

您只需按照以下步骤操作即可:

  1. 添加以下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. 使用NSURLSession下载您的GIF

  3. 显示

    UIImage *image = GIFFromData(data);
    UIImageView *view = [[UIImageView alloc] initWithImage:image];
    
  4. 希望这会有所帮助:)

答案 1 :(得分: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