你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> cocoa ios iphone xcode 播放GIF動畫

cocoa ios iphone xcode 播放GIF動畫

編輯:IOS開發綜合

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


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved