美文网首页
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;
    }
    

    相关文章

      网友评论

          本文标题:iOS Gif图片加载

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