iOS中UIImageView显示GIF图片

#import "ViewController.h"

#import <ImageIO/ImageIO.h>

 

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

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

    

    

    

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"0302.gif" withExtension:nil];

    CGImageSourceRef csf = CGImageSourceCreateWithURL((__bridge CFTypeRef)url, NULL);

    size_t const count = CGImageSourceGetCount(csf);

    UIImage *frames[count];

    CGImageRef images[count];

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

        images[i] = CGImageSourceCreateImageAtIndex(csf, i, NULL);

        UIImage *image =[[UIImage alloc] initWithCGImage:images[i]];

        frames[i] = image;

        CFRelease(images[i]);

    }

    

    UIImage *const animation = [UIImage animatedImageWithImages:[NSArray arrayWithObjects:frames count:count] duration:1.0]; // 可以设置gif的播放时间

    [self.iconView setImage:animation];

    CFRelease(csf);

}

 

 

 

posted on 2016-03-02 17:22  18733540977  阅读(843)  评论(0)    收藏  举报

导航