iOS 播放GIF动画

来源:互联网 发布:java byte转16进制 编辑:程序博客网 时间:2024/05/21 02:21

1.使用UIWebView的播放

#pragma clang diagnostic ignored "-Wnonnull"    NSString *path = [[NSBundle mainBundle] pathForResource:@"<#gifName#>" ofType:@"gif"];    NSData *gifData = [NSData dataWithContentsOfFile:path];    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(<#x#>, <#y#>, <#w#>, <#h#>)];    webView.scalesPageToFit = YES;    [webView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];    webView.backgroundColor = [UIColor clearColor];    webView.opaque = NO;    [self.view addSubview:webView];

2.将GIF图片分解成多张PNG图片,使用的UIImageView播放。

需要导入#import <ImageIO/ImageIO.h>

    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"<#gifName#>" withExtension:@"gif"]; //加载GIF图片    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef) fileUrl, NULL);           //将GIF图片转换成对应的图片源    size_t frameCout = CGImageSourceGetCount(gifSource);                                         //获取其中图片源个数,即由多少帧图片组成    NSMutableArray *frames = [[NSMutableArray alloc] init];                                      //定义数组存储拆分出来的图片    for (size_t i = 0; i < frameCout; i++) {        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL); //从GIF图片中取出源图片        UIImage *imageName = [UIImage imageWithCGImage:imageRef];                  //将图片源转换成UIimageView能使用的图片源        [frames addObject:imageName];                                              //将图片加入数组中        CGImageRelease(imageRef);    }    UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(<#x#>, <#y#>, <#w#>, <#h#>)];    gifImageView.animationImages = frames; //将图片数组加入UIImageView动画数组中    gifImageView.animationDuration = 0.15; //每次动画时长    [gifImageView startAnimating];         //开启动画,此处没有调用播放次数接口,UIImageView默认播放次数为无限次,故这里不做处理    [self.view addSubview:gifImageView];

3.如何播放的NSData数据的GIF

+ (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;}
至于哪种方式性能好呢?

使用的UIWebView性能会好点,的UIImageView播放是通过定时器来控制图片模拟动画的,它们控制的桢速是固定的。如果设置的模拟桢速跟GIF本身的桢速相近的话倒没什么,如果桢速相差过大就很耗性能。

如果想实现gif图片的暂停和继续播放的功能加入以下两个方法就可以了

1.首先再viewDidLoad加入这个(player是CALayer类型的,定义为全局的)

//    设置imageView的layer
    player = self.loadingImageView.layer;

2.实现这两个方法,需要用哪个就点击哪个直接把player传过去就可以了

//暂停gif的方法

-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

//继续gif的方法
-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] -    pausedTime;
    layer.beginTime = timeSincePause;
}


原创粉丝点击