SDWebImage 加载Gif

UIImage+MultiFormat


+ (nullable UIImage *)sd_imageWithData:(nullable NSData *)data {

    if (!data) {

        return nil;

    }

    

    UIImage *image;

    SDImageFormat imageFormat = [NSData sd_imageFormatForImageData:data];

    if (imageFormat == SDImageFormatGIF) {

        image = [UIImage sd_animatedGIFWithData:data];

    }

#ifdef SD_WEBP

    else if (imageFormat == SDImageFormatWebP)

    {

        image = [UIImage sd_imageWithWebPData:data];

    }

#endif

    else {

        image = [[UIImage alloc] initWithData:data];

#if SD_UIKIT || SD_WATCH

        UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];

        if (orientation != UIImageOrientationUp) {

            image = [UIImage imageWithCGImage:image.CGImage

                                        scale:image.scale

                                  orientation:orientation];

        }

#endif

    }



    return image;

}


UIImage+GIF


+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {

    if (!data) {

        return nil;

    }


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


    size_t count = CGImageSourceGetCount(source);


    UIImage *staticImage;

    /*

    if (count <= 1) {

        staticImage = [[UIImage alloc] initWithData:data];

    } else {

        // we will only retrieve the 1st frame. the full GIF support is available via the FLAnimatedImageView category.

        // this here is only code to allow drawing animated images as static ones

#if SD_WATCH

        CGFloat scale = 1;

        scale = [WKInterfaceDevice currentDevice].screenScale;

#elif SD_UIKIT

        CGFloat scale = 1;

        scale = [UIScreen mainScreen].scale;

#endif

        

        CGImageRef CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);

#if SD_UIKIT || SD_WATCH

        UIImage *frameImage = [UIImage imageWithCGImage:CGImage scale:scale orientation:UIImageOrientationUp];

        staticImage = [UIImage animatedImageWithImages:@[frameImage] duration:0.0f];

#elif SD_MAC

        staticImage = [[UIImage alloc] initWithCGImage:CGImage size:NSZeroSize];

#endif

        CGImageRelease(CGImage);

    }*/

    

    if (count <= 1) {

        staticImage = [[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);

            

            duration += [self frameDurationAtIndex:i source:source];

            

            [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];

            

            CGImageRelease(image);

        }

        

        if (!duration) {

            duration = (1.0f / 10.0f) * count;

        }

        

        staticImage = [UIImage animatedImageWithImages:images duration:duration];

    }

    

    CFRelease(source);


    return staticImage;

}


- (BOOL)isGIF {

    return (self.images != nil);

}


+ (float)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {

    float frameDuration = 0.1f;

    CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);

    NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;

    NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];

    

    NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];

    if (delayTimeUnclampedProp) {

        frameDuration = [delayTimeUnclampedProp floatValue];

    }

    else {

        

        NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];

        if (delayTimeProp) {

            frameDuration = [delayTimeProp floatValue];

        }

    }

    

    if (frameDuration < 0.011f) {

        frameDuration = 0.100f;

    }

    

    CFRelease(cfFrameProperties);

    return frameDuration;

}


Pod升级SDWebImage


pod 'SDWebImage', '~> 4.0.0'

pod 'FLAnimatedImage', '~> 1.0'

pod 'SDWebImage/GIF'



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值