• iOS GIF图片转UIImage


    多平台保持统一风格的UI设计,少不了一些动态图片的使用

    1、本地GIF图片使用

     1.1 将本地GIF转为NSdata类型

    1  NSData *tempdata = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"networkLoading" ofType:@"gif"]];
    2  UIImageView *imgView = [[UIImageView alloc] initWithImage:[self decodeGifImageByData:tempdata]];

     1.2 通过自定义方法处理GIF的data数据

     1 //解压gif的data为图片数组
     2 -(UIImage *)decodeGifImageByData:(NSData *)data {
     3     //获取data资源器,这个可以直接操作图片data
     4     CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, nil);
     5     //获取图片数量 size_t 类似于无符号int
     6     size_t count = CGImageSourceGetCount(source);
     7     UIImage * animationImage;
     8     
     9     //如果只有一张则直接解压
    10     if (count <= 1) {
    11         animationImage = [[UIImage alloc]initWithData:data];
    12     }
    13     else {
    14         NSMutableArray * imageArray = [NSMutableArray arrayWithCapacity:count];
    15         NSTimeInterval duration = 0.0f;
    16         //遍历获取每一张图片
    17         for (size_t i = 0; i < count; i++) {
    18             //解析图片拿到图片画笔拿到图片画笔
    19             CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
    20             duration += [self imageDurationAtIndex:i source:source];
    21             //scale:图片缩放因子 默认1  orientation:图片绘制方向 默认网上
    22             [imageArray addObject:[UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
    23             CGImageRelease(imageRef);
    24         }
    25         
    26         //如果没有抓取到图片播放时间,则按照0.1秒一张的方式来了播放
    27         if (!duration) {
    28             duration = (1.0f / 10.0f) * count;
    29         }
    30         animationImage = [UIImage animatedImageWithImages:imageArray duration:duration];
    31     }
    32     
    33     
    34     CFRelease(source);
    35     return animationImage;
    36 }
    37 
    38 //获取每一张图片的持续时间
    39 -(float)imageDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
    40     float imageDuration = 0.1f;
    41     //获取指定图像的属性信息 如宽、高、持续时间等都在里面 详情参考 CGImageProperties
    42     CFDictionaryRef cfImageProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
    43     if (!cfImageProperties) {
    44         return imageDuration;
    45     }
    46     NSDictionary * imageProperties = (__bridge NSDictionary *) cfImageProperties;
    47     //拿到gif图的属性信息 获取每一帧的时间
    48     NSDictionary * gifProperties = [imageProperties valueForKey:(NSString *)kCGImagePropertyGIFDictionary];
    49     NSNumber * delayTime = [gifProperties valueForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
    50     if (delayTime != nil) {
    51         return delayTime.floatValue;
    52     }
    53     
    54     return imageDuration;
    55 }
  • 相关阅读:
    c++ 小记
    扁平化设计
    json 学习
    c#多线程下载
    php 序列化储存和转化 json_encode() json_decode($q,true)
    支付接口
    JSON.stringify 语法实例讲解 字符串
    jquery 单选框整个选中
    js戳和php戳时间换算
    windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes 解决方案
  • 原文地址:https://www.cnblogs.com/zxs-19920314/p/12777867.html
Copyright © 2020-2023  润新知