← 返回目录

cocoa ios iphone xcode 播放GIF动画

发布时间: 2012-12-06 10:24:12
浏览量: 10321
收藏数: 3
标签: cocoa, ios, iOS, IOS, iphone, iPhone, IPhone, IPHONE, opensource, xcode, Xcode, XCode
使用imageio的这个自带的framework, 这个库也是apple的webkit所使用的,可以参考apple的opensource的webkit实现。 因此,这个 库从性能和蒹容性方面应该都是最佳选择以下是代码,比较简单 NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObjectsAndKeys:

使用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]