美文网首页
iOS Gif图片加载

iOS Gif图片加载

作者: 站在下一刻 | 来源:发表于2018-11-13 18:51 被阅读38次

Gif图片如越来越受欢迎,移动端对它的支持也是有些知识点的,主要是加载图片性能优化

  1. Gif图片的生成

Gif图片是一个图片数组,iOS的imageWithNamed方法只会取图片的第一帧,并不会显示动图,需要用到UIImage的animatedImageWithImages:duration:方法,第一参数传包含Gif每一帧图片的数组,第二个参数传Gif图片一次播放的时间,

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

通过图片data获取source;

size_t count = CGImageSourceGetCount(source);

if (count <= 1) 

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

如果count就是一张,就直接initWithData返回;否则,

NSMutableArray *images = [NSMutableArray array];
    
    NSTimeInterval duration = 0.0f;
    
    for (size_t i = 0; i < count; i++) {
        
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
        
        if (!imageRef) {
            continue;
        }
        
        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];
            }
            
        }
        
        CFRelease(cfFrameProperties);
        
        duration += frameDuration;
        
        [images addObject:[UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
        
        CGImageRelease(imageRef);
        
    }
    
    if (!duration) {
        duration = (1.0f / 10.0f) * count;
    }
    
    animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    CFRelease(source);

至此,Gif的动画图片animatedImage就生成了

  1. GIf图片展示性能优化

由于iOS在处理图片需要对它进行解压缩,这个过程默认在主线程,而且也是非常耗时间,耗资源,内存占用会增加,那么当需要加载的图片比较多时,比如Gif图片,就会对我们应用的响应性造成严重的影响,特别是在滑动的tableView上,这个问题会表现得很明显,所以在返回图片的时候最好将它解压缩好。

参考SDWebImage中的解压缩代码,只需要将上述imageRef在转化UIImage之前进行处理一下就好了,

@autoreleasepool {
        
        CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
        
        CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
        
        BOOL unsupportedColorSpace = (imageColorSpaceModel == kCGColorSpaceModelUnknown ||
                                      
                                      imageColorSpaceModel == kCGColorSpaceModelMonochrome ||
                                      
                                      imageColorSpaceModel == kCGColorSpaceModelCMYK ||
                                      
                                      imageColorSpaceModel == kCGColorSpaceModelIndexed);
        
        if (unsupportedColorSpace) {
            
            colorspaceRef = CGColorSpaceCreateDeviceRGB();
            
            CFAutorelease(colorspaceRef);
            
        }
        
        size_t width = CGImageGetWidth(imageRef);
        
        size_t height = CGImageGetHeight(imageRef);
        
        size_t bytesPerRow = 4 * width;
        
        CGContextRef context = CGBitmapContextCreate(NULL,width,height,8,bytesPerRow,colorspaceRef,kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast);
        
        if (context != NULL) {
            
            CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
            
            CGImageRef imageRefWithoutAlpha = CGBitmapContextCreateImage(context);
            
            CGContextRelease(context);
            
            CGImageRelease(imageRef);
            
            imageRef = imageRefWithoutAlpha;
            
        }
        
    }

这样获取的imageRef就是解压缩好的图片了。

  1. 通过以上两步结合就可以比较好的支持Gif图片的展示了,如下;
- (UIImage *)gifImageWithData:(NSData *)data{
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    size_t count = CGImageSourceGetCount(source);
    if (count <= 1) {
        return [[UIImage alloc] initWithData:data];
    }
    
    NSMutableArray *images = [NSMutableArray array];
    
    NSTimeInterval duration = 0.0f;
    
    for (size_t i = 0; i < count; i++) {
        
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
        
        if (!imageRef) {
            continue;
        }
        
        @autoreleasepool {
            
            CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
            
            CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
            
            BOOL unsupportedColorSpace = (imageColorSpaceModel == kCGColorSpaceModelUnknown ||
                                          
                                          imageColorSpaceModel == kCGColorSpaceModelMonochrome ||
                                          
                                          imageColorSpaceModel == kCGColorSpaceModelCMYK ||
                                          
                                          imageColorSpaceModel == kCGColorSpaceModelIndexed);
            
            if (unsupportedColorSpace) {
                
                colorspaceRef = CGColorSpaceCreateDeviceRGB();
                
                CFAutorelease(colorspaceRef);
                
            }
            
            size_t width = CGImageGetWidth(imageRef);
            
            size_t height = CGImageGetHeight(imageRef);
            
            size_t bytesPerRow = 4 * width;
            
            CGContextRef context = CGBitmapContextCreate(NULL,width,height,8,bytesPerRow,colorspaceRef,kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast);
            
            if (context != NULL) {
                
                CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
                
                CGImageRef imageRefWithoutAlpha = CGBitmapContextCreateImage(context);
                
                CGContextRelease(context);
                
                CGImageRelease(imageRef);
                
                imageRef = imageRefWithoutAlpha;
                
            }
            
        }
        
        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];
            }
            
        }
        
        CFRelease(cfFrameProperties);
        
        duration += frameDuration;
        
        [images addObject:[UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
        
        CGImageRelease(imageRef);
        
    }
    
    if (!duration) {
        duration = (1.0f / 10.0f) * count;
    }
    
    UIImage *animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    
    CFRelease(source);
    
    return animatedImage;
}

相关文章

  • Swift (四) gif图片播放

    @[TOC](IOS gif图片播放 swift) 1. GIF在iOS平台上的几种加载方式 使用Dispatch...

  • iOS SDWebImage 加载GIF图片没效果

    //加载本地GIF图片 //加载网络GIF图片 (SDWebImage 4.0 之前) //加载网络GIF图片(S...

  • Android Glide 使用

    加载 GIF 图片到 ImageView 中 通常 Android 的 ImageView 不能加载 Gif 图片...

  • Xcode开发播放gif图片

    在开发iOS程序的过程中,可能会遇到要加载gif图片,下面介绍gif图片的加载方式:先介绍一个第三方:gifVie...

  • iOS Gif图片加载

    Gif图片如越来越受欢迎,移动端对它的支持也是有些知识点的,主要是加载图片性能优化 Gif图片的生成 Gif图片是...

  • iOS加载gif图片

    1、通过拆分gif,加载一个图片数组(推荐一个Mac APP自动拆分gif:Gif Preview) 2、UIWe...

  • iOS加载Gif图片

    Gif图片是非常常见的图片格式,尤其是在聊天的过程中,Gif表情使用地很频繁。但是iOS竟然没有现成的支持加载和播...

  • iOS 加载gif图片

    一,UIWebview加载 核心代码如下: 注意:上面的xib也就是,登录自定义view要在webview请求完之...

  • ios加载GIF图片

    原生的UIImageView是不支持gif格式图片的,以下是我总结的三种方法,希望可以帮助到你。 一、用UIWeb...

  • iOS加载Gif图片

    1.系统UIImageView 多张图片组成动画 2.利用第三方库 IImageView-PlayGIF YFGI...

网友评论

      本文标题:iOS Gif图片加载

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