ios播放gif动画

来源:互联网 发布:剑三花爹数据 编辑:程序博客网 时间:2024/05/21 04:24

#import <ImageIO/ImageIO.h>

 NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@“xxx” ofType:@"gif"]];
        NSMutableArray *frames = nil;
        
        CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)gifData, NULL);
        double total = 0;
        NSTimeInterval gifAnimationDuration;
        if (src) {
            size_t l = CGImageSourceGetCount(src);
            if (l > 1){
                frames = [NSMutableArray arrayWithCapacity: l];
                for (size_t i = 0; i < l; i++) {
                    CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
                    NSDictionary *dict =  (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(src, 0, NULL));
                    if (dict){
                        NSDictionary *tmpdict = [dict objectForKey: @"{GIF}"];
                        total += [[tmpdict objectForKey: @"DelayTime"] doubleValue] * 100;
                    }
                    if (img) {
                        [frames addObject: [UIImage imageWithCGImage: img]];
                        CGImageRelease(img);
                    }
                }
                gifAnimationDuration = total / 100;
                
                UIImageView *imageView = [[UIImageView alloc] init];
                imageView.animationImages = frames;
                imageView.animationDuration = gifAnimationDuration;
                [imageView startAnimating];
                imageView.frame=CGRectMake((i-1)*[UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
   
                
                [pageContairView addSubview:imageView];
            }
        }


0 0
原创粉丝点击