2017-05-05 66 views
-4

我想顯示GIF動畫,但它沒有工作,他們保持不動。Objecive C - 如何播放動畫GIF圖像

UIView * tak = [[UIView alloc] initWithFrame:CGRectMake(-10, 0, 560, 685)]; 
    UIImage *image = [UIImage imageNamed:@"animated-f.gif"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    imageView.frame = tak.bounds; 
    [tak addSubview:imageView]; 
    [self.view addSubview:tak]; 

感謝

+1

喜@ user460024你可以參考此鏈接可能是有益的http://stackoverflow.com/questions/39870379/gif-image-not-show –

+0

[顯示器的可能的複製iOS中的動畫GIF](http://stackoverflow.com/questions/9744682/display-animated-gif-in-ios) – Larme

回答

0

使用FLAnimatedImage

#import "FLAnimatedImage.h" 

NSURL *url = [[NSBundle mainBundle] URLForResource:@"animated-f" withExtension:@"gif"]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data]; 
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init]; 
imageView.animatedImage = image; 
imageView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0); 
[self.view addSubview:imageView]; 
0

我覺得這個評判在UIImage+GIFSDWebImage類可以在之前版本3.7.2播放GIF,但這種方法佔用了多少內存,在版本sdFLAnimatedImage來代替它,解決內存過大的問題。建議使用FLAnimatedImage

version 3.7.2 code

- (UIImage *)sd_animatedGIFWithData:(NSData *)data { 
if (!data) { 
    return nil; 
} 

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); 

size_t count = CGImageSourceGetCount(source); 

UIImage *animatedImage; 

if (count <= 1) { 
    animatedImage = [[UIImage alloc] initWithData:data]; 
} 
else { 
    NSMutableArray *images = [NSMutableArray array]; 

    NSTimeInterval duration = 0.0f; 

    for (size_t i = 0; i < count; i++) { 
     CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); 

     duration += [self sd_frameDurationAtIndex:i source:source]; 

     [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]]; 

     CGImageRelease(image); 
    } 

    if (!duration) { 
     duration = (1.0f/10.0f) * count; 
    } 

    animatedImage = [UIImage animatedImageWithImages:images duration:duration]; 
} 

CFRelease(source); 

return animatedImage; 
} 

- (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source { 
float frameDuration = 0.1f; 
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil); 
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties; 
NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary]; 

NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime]; 
if (delayTimeUnclampedProp) { 
    frameDuration = [delayTimeUnclampedProp floatValue]; 
} 
else { 

    NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime]; 
    if (delayTimeProp) { 
     frameDuration = [delayTimeProp floatValue]; 
    } 
} 

// Many annoying ads specify a 0 duration to make an image flash as quickly as possible. 
// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify 
// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082> 
// for more information. 

if (frameDuration < 0.011f) { 
    frameDuration = 0.100f; 
} 

CFRelease(cfFrameProperties); 
return frameDuration; 
}