美文网首页
GIF动画加载

GIF动画加载

作者: 赤焰军少帅林殊 | 来源:发表于2018-01-03 21:08 被阅读39次
    #pragma mark - Gif
    /**
     *  播放动画
     *
     *  @param data 源文件(图片源)
     *
     *  @return
     */
    + (UIImage *)animatedGIFWithData:(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++) {
                
                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;
            }
            // 加载动画图片,指定动画播放时间
            animatedImage = [UIImage animatedImageWithImages:images duration:duration];
        }
        
        CFRelease(source);
        
        return animatedImage;
    }
    
    /**
     *  计算动画中每一张图片的播放时间
     *
     *  @param index  图片索引
     *  @param source 图片组
     *
     *  @return  播放时间
     */
    + (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;
    }
    
    /**
     *  播放gif动画
     *
     *  @param name 文件名
     *
     *  @return
     */
    + (UIImage *)animatedGIFNamed:(NSString *)name {
        CGFloat scale = [UIScreen mainScreen].scale;
        
        // 视网膜屏,可能要加载高清图
        if (scale > 1.0f) {
            // 文件名1
            NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];
            
            NSData *data = [NSData dataWithContentsOfFile:retinaPath];
            
            if (data) {
                return [UIImage animatedGIFWithData:data];
            }
            
            // 文件名2
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
            
            data = [NSData dataWithContentsOfFile:path];
            
            if (data) {
                return [UIImage animatedGIFWithData:data];
            }
            
            return [UIImage imageNamed:name];
        }
        // 普通屏幕
        else {
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
            
            NSData *data = [NSData dataWithContentsOfFile:path];
            
            if (data) {
                return [UIImage animatedGIFWithData:data];
            }
            
            return [UIImage imageNamed:name];
        }
    }
    
    /**
     *  缩放动画
     *
     *  @param size 大小
     *
     *  @return  
     */
    - (UIImage *)animatedImageByScalingAndCroppingToSize:(CGSize)size {
        if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) {
            return self;
        }
        
        CGSize scaledSize = size;
        CGPoint thumbnailPoint = CGPointZero;
        
        CGFloat widthFactor = size.width / self.size.width;
        CGFloat heightFactor = size.height / self.size.height;
        CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor : heightFactor;
        scaledSize.width = self.size.width * scaleFactor;
        scaledSize.height = self.size.height * scaleFactor;
        
        if (widthFactor > heightFactor) {
            thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
        }
        else if (widthFactor < heightFactor) {
            thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
        }
        
        NSMutableArray *scaledImages = [NSMutableArray array];
        
        // 重绘制图片
        UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
        for (UIImage *image in self.images) {
            [image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
            UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
            
            [scaledImages addObject:newImage];
        }
        
        UIGraphicsEndImageContext();
        
        return [UIImage animatedImageWithImages:scaledImages duration:self.duration];
    }
    

    相关文章

      网友评论

          本文标题:GIF动画加载

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