美文网首页
关于使用SDWebImage加载gif图片遇到的坑

关于使用SDWebImage加载gif图片遇到的坑

作者: 碎梦_aimee | 来源:发表于2017-05-24 16:40 被阅读0次

    之前使用的是的SDWebImage来加载的gif图片,后来更新了SDWebImage,gif图片就无法使用,只能默认选择了第一张图片。

    查看了新的SDWebImage中的UIImage+GIF,发现原来最新的代码中做了限制+ (UIImage *)sd_animatedGIFWithData:(NSData *)data方法中限制使用gif图片了,必须要使用FLAnimatedImageView三方的imagview展示才可以。

    如果使用老版本的SDWebImage中的uiimage + gif类,当你调用+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;方法的时候,他会默认调最新的+ (UIImage *)sd_animatedGIFWithData:(NSData *)data这个方法。(我用的是pods)

    还有一种解决办法就是,我更换了uiimage+gif方法(就解决了……)


    #import  <UIKit/UIKit.h>

    typedef void (^GIFimageBlock)(UIImage *GIFImage);

    @interface UIImage (GIF)

    /** 根据本地GIF图片名 获得GIF image对象 */

    + (UIImage *)imageWithGIFNamed:(NSString *)name;

    /** 根据一个GIF图片的data数据 获得GIF image对象 */

    + (UIImage *)imageWithGIFData:(NSData *)data;

    /** 根据一个GIF图片的URL 获得GIF image对象 */

    + (void)imageWithGIFUrl:(NSString *)url and:(GIFimageBlock)gifImageBlock;

    @end


    #import "UIImage+GIF.h"

    #import <ImageIO/ImageIO.h>

    @implementation UIImage (GIF)

    + (UIImage *)imageWithGIFData:(NSData *)data

    {

    if (!data) return nil;

    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++) {

    // 拿出了Gif的每一帧图片

    CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);

    //Learning... 设置动画时长 算出每一帧显示的时长(帧时长)

    NSTimeInterval frameDuration = [UIImage sd_frameDurationAtIndex:i source:source];

    duration += frameDuration;

    // 将每帧图片添加到数组中

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

    // 释放真图片对象

    CFRelease(image);

    }

    // 设置动画时长

    if (!duration) {

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

    }

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

    }

    // 释放源Gif图片

    CFRelease(source);

    return animatedImage;

    }

    + (UIImage *)imageWithGIFNamed:(NSString *)name

    {

    NSUInteger scale = (NSUInteger)[UIScreen mainScreen].scale;

    return [self GIFName:name scale:scale];

    }

    + (UIImage *)GIFName:(NSString *)name scale:(NSUInteger)scale

    {

    NSString *imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@%zdx", name, scale] ofType:@"gif"];

    if (!imagePath) {

    (scale + 1 > 3) ? (scale -= 1) : (scale += 1);

    imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@%zdx", name, scale] ofType:@"gif"];

    }

    if (imagePath) {

    // 传入图片名(不包含@Nx)

    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];

    return [UIImage imageWithGIFData:imageData];

    } else {

    imagePath = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];

    if (imagePath) {

    // 传入的图片名已包含@Nx or 传入图片只有一张 不分@Nx

    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];

    return [UIImage imageWithGIFData:imageData];

    } else {

    // 不是一张GIF图片(后缀不是gif)

    return [UIImage imageNamed:name];

    }

    }

    }

    + (void)imageWithGIFUrl:(NSString *)url and:(GIFimageBlock)gifImageBlock

    {

    NSURL *GIFUrl = [NSURL URLWithString:url];

    if (!GIFUrl) return;

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

    NSData *CIFData = [NSData dataWithContentsOfURL:GIFUrl];

    // 刷新UI在主线程

    dispatch_async(dispatch_get_main_queue(), ^{

    gifImageBlock([UIImage imageWithGIFData:CIFData]);

    });

    });

    }

    #pragma mark - <关于GIF图片帧时长(Learning...)>

    + (float)sd_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];

    }

    }

    // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.

    // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify

    // a duration of <= 10 ms. See and

    // for more information.

    if (frameDuration < 0.011f) {

    frameDuration = 0.100f;

    }

    CFRelease(cfFrameProperties);

    return frameDuration;

    }

    @end

    相关文章

      网友评论

          本文标题:关于使用SDWebImage加载gif图片遇到的坑

          本文链接:https://www.haomeiwen.com/subject/qpdoxxtx.html