cocoa ios iphone xcode 播放GIF动画

来源:互联网 发布:我的校园软件 编辑:程序博客网 时间:2024/06/03 15:31

使用imageio的这个自带的framework, 这个库也是apple的webkit所使用的,可以参考apple的opensource的webkit实现。 因此,这个 库从性能和蒹容性方面应该都是最佳选择

以下是代码,比较简单 


NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObjectsAndKeys:                                  [NSNumber numberWithInt:0] , (NSString *)kCGImagePropertyGIFLoopCount,nil                                  ];        NSDictionary * gifProperties = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary] ;        CGImageSourceRef gif = CGImageSourceCreateWithData((__bridge  CFDataRef)(data), (__bridge  CFDictionaryRef)gifProperties);            CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));        NSInteger count =CGImageSourceGetCount(gif);        CFDictionaryRef  gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary);//[gifprops objectForKey:(NSString *)kCGImagePropertyGIFDictionary];        NSNumber * delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime); //[gifDic objectForKey:(NSString *)kCGImagePropertyGIFDelayTime];    NSNumber * w = CFDictionaryGetValue(gifprops, @"PixelWidth");    NSNumber * h =CFDictionaryGetValue(gifprops, @"PixelHeight");    totalDuration  = delay.doubleValue * count;    pixelWidth = w.intValue;    pixelHeight = h.intValue;          images = [[NSMutableArray alloc] init];    for(int index=0;index<count;index++)    {        CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, nil);        UIImage *img = [UIImage imageWithCGImage:ref];        [images addObject:img];        CFRelease(ref);    }        CFRelease(gifprops);    CFRelease(gif);

解压完之后,直接用IMageview就可以播放了。代码如下。

    UIImageView *image = ....; 
    image.animationDuration = totalDuration;    image.animationImages = images;    [image startAnimating]