zoukankan      html  css  js  c++  java
  • ios开发调用gif动态图

    //

    //  ViewController.m

    //  demoLaoyang

    //

    //  Created by dongqiangfei on 16/7/18.

    //  Copyright © 2016年 dongqiangfei. All rights reserved.

    //

    #import "ViewController.h"

    #import <ImageIO/ImageIO.h>

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        NSString  *name = @"loading.gif";

        NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];

        NSData  *imageData = [NSData dataWithContentsOfFile:filePath];

        UIImageView *loadingImageView = [[UIImageView alloc] init];

        loadingImageView.backgroundColor = [UIColor clearColor];

        loadingImageView.image = [self gifChangeToImageWithData:imageData];

        loadingImageView.frame = CGRectMake(0, 0, 124, 124);

        

        [self configUI:loadingImageView];

        

        // Do any additional setup after loading the view, typically from a nib.

    }

    - (UIImage *)gifChangeToImageWithData:(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 = 3.0f;

            

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

            {

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

                if (!image)

                {

                    continue;

                }

                

                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;

    }

    - (void)configUI:(UIImageView *)loadingImageView

    {

        

        loadingImageView.center = CGPointMake(400 / 2, 400 / 2);

        loadingImageView.tag = 0xadd;

        [self.view addSubview:loadingImageView];

        

    }

    - (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;

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

  • 相关阅读:
    HBASE学习笔记(一)
    模板:循环数据库表
    where(泛型类型约束)
    如何很好的使用Linq的Distinct方法
    Sql自定义表类型批量导入数据
    Linq select 语法
    JTemplate学习(四)
    JTemplate学习(三)
    JTemplate学习(二)
    正则表达式学习
  • 原文地址:https://www.cnblogs.com/godlovexq/p/5692031.html
Copyright © 2011-2022 走看看