天天看点

iOS 两种加载GIF图片区别

现在开发不论是广告页,新手引导图或者发送图片都有GIF了,但是很多时候不同的加载对内存也有不同的负担

一般大家现在都用WKWebView或者SDWebImage,YYImage来加载比较对内存不会占用那么多

例如我一般都是用WKWebView,不过听说YYImage对gif图播放支持比较好点,你有空可以试试那个。

 // 读取gif图片数据

 NSData *gif = [NSDatadataWithContentsOfFile: [[NSBundlemainBundle] pathForResource:@"angelBird"ofType:@"gif"]];

 // webview生成

 WKWebView *webView = [[WKWebViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height)];

 [webView loadData:gifMIMEType:@"image/gif"characterEncodingName:nilbaseURL:nil];

 [self.viewaddSubview:webView];

另一种就是看着别人然后自己也写了,原生的了,继承ImageView

@interface RQimageView : UIImageView

-(void)yh_setImage:(NSURL *)imageUrl;

@end

#import "RQimageView.h"

#import <ImageIO/ImageIO.h>

@implementation RQimageView

-(void)getGifImageWithUrk:(NSURL *)url

               returnData:(void(^)(NSArray<UIImage *> * imageArray,

                                   NSArray<NSNumber *>*timeArray,

                                   CGFloat totalTime,

                                   NSArray<NSNumber *>* widths,

                                   NSArray<NSNumber *>* heights))dataBlock{

    //通过文件的url来将gif文件读取为图片数据引用

    CGImageSourceRef source =CGImageSourceCreateWithURL((CFURLRef)url,NULL);

    //获取gif文件中图片的个数

    size_t count =CGImageSourceGetCount(source);

    //定义一个变量记录gif播放一轮的时间

    float allTime=0;

    //存放所有图片

    NSMutableArray * imageArray = [[NSMutableArrayalloc]init];

    //存放每一帧播放的时间

    NSMutableArray * timeArray = [[NSMutableArrayalloc]init];

    //存放每张图片的宽度(一般在一个gif文件中,所有图片尺寸都会一样)

    NSMutableArray * widthArray = [[NSMutableArrayalloc]init];

    //存放每张图片的高度

    NSMutableArray * heightArray = [[NSMutableArrayalloc]init];

    //遍历

    for (size_t i=0; i<count; i++) {

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

        [imageArray addObject:(__bridgeUIImage *)(image)];

        CGImageRelease(image);

        //获取图片信息

        NSDictionary * info = (__bridgeNSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, i,NULL);

        CGFloat width = [[infoobjectForKey:(__bridgeNSString *)kCGImagePropertyPixelWidth]floatValue];

        CGFloat height = [[infoobjectForKey:(__bridgeNSString *)kCGImagePropertyPixelHeight]floatValue];

        [widthArray addObject:[NSNumbernumberWithFloat:width]];

        [heightArray addObject:[NSNumbernumberWithFloat:height]];

        NSDictionary * timeDic = [infoobjectForKey:(__bridgeNSString *)kCGImagePropertyGIFDictionary];

        CGFloat time = [[timeDicobjectForKey:(__bridgeNSString *)kCGImagePropertyGIFDelayTime]floatValue];

        allTime+=time;

        [timeArray addObject:[NSNumbernumberWithFloat:time]];

        CFRelease((__bridgeCFTypeRef)(info));

    }

    CFRelease(source);

    dataBlock(imageArray,timeArray,allTime,widthArray,heightArray);

}

-(void)yh_setImage:(NSURL *)imageUrl{

    __weakid __self = self;

    [selfgetGifImageWithUrk:imageUrlreturnData:^(NSArray<UIImage *> *imageArray,NSArray<NSNumber *> *timeArray,CGFloat totalTime, NSArray<NSNumber *> *widths,NSArray<NSNumber *> *heights) {

        //添加帧动画

        CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"contents"];

        NSMutableArray * times = [[NSMutableArrayalloc]init];

        float currentTime =0;

        //设置每一帧的时间占比

        for (int i=0; i<imageArray.count; i++) {

            [times addObject:[NSNumbernumberWithFloat:currentTime/totalTime]];

            currentTime+=[timeArray[i] floatValue];

        }

        [animation setKeyTimes:times];

        [animation setValues:imageArray];

        [animation setTimingFunction:[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear]];

        //设置循环

        animation.repeatCount=MAXFLOAT;

        //设置播放总时长

        animation.duration = totalTime;

        //Layer层添加

        [[(UIImageView *)__selflayer]addAnimation:animationforKey:@"gifAnimation"];

    }];

}

@end

调用

   NSURL * url = [[NSURLalloc]initFileURLWithPath:[[NSBundlemainBundle] pathForResource:@"angelBird.gif"ofType:nil]];

    RQimageView *imgView = [[RQimageViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height)];

    [imgView yh_setImage:url];

    [self.viewaddSubview:imgView];

我开发的时候对着两种做过比较,开始觉得自己写了用特别有成就感,然后发现用第二种内存飙到103MB

iOS 两种加载GIF图片区别

然而用WKwebView还好,才11MB左右,原因你看代码也知道啦,这样循环加载播放,不彪才怪,哈哈