ios开发调用gif动态图

版权声明: https://blog.csdn.net/qq_35122556/article/details/80020356

//

//  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

猜你喜欢

转载自blog.csdn.net/qq_35122556/article/details/80020356