iOS加载gif图片

1、通过拆分gif,加载一个图片数组(推荐一个Mac APP自动拆分gif:Gif Preview)

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,   100, 120, 70)];
    imageView.backgroundColor = [UIColor orangeColor];
    NSMutableArray *marr = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 12; i++) {
        NSString *string = [NSString stringWithFormat:@"img%d.png",i];
        UIImage *image = [UIImage imageNamed:string];
        [marr addObject:image];
    }
    imageView.animationImages = marr;
    imageView.animationDuration = 1;
    imageView.animationRepeatCount = 0;
    [imageView startAnimating];
    [self.view addSubview:imageView];

2、UIWebView加载gif

    CGRect frame = CGRectMake(100, 250, 0, 0);
    frame.size = [UIImage imageNamed:@"gundong.gif"].size;
    
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"gundong" ofType:@"gif"]];
    UIWebView *web = [[UIWebView alloc] initWithFrame:frame];
    web.userInteractionEnabled = NO;
    [web loadData:data MIMEType:@"image/gif" textEncodingName:@"" baseURL:[NSURL URLWithString:@""]];
    [self.view addSubview:web];

3、SDWebImage加载gif(4.0.0之前版本)

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 120, 70)];
    imageView.image = [UIImage sd_animatedGIFNamed:@"gundong"];
    [self.view addSubview:imageView];

4、FLAnimationImage加载gif(SDWebImage 4.0.0版本)

  • pod 'SDWebImage/GIF'即可导入FLAnimationImage
    4.0.0版本 UIImage+GIF.m 中也做了解释:


    UIImage+GIF.m

    意思就是:我们将只检索第一帧。完整的GIF支持可通过flanimatedimageview。

    CGRect frame = CGRectMake(100, 250, 0, 0);
    frame.size = [UIImage imageNamed:@"gundong.gif"].size;
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"gundong" ofType:@"gif"]];

    FLAnimatedImageView *flView = [[FLAnimatedImageView alloc] initWithFrame:CGRectMake(100, 400, frame.size.width, frame.size.height)];
    flView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];
    flView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:flView];

[SDWebImage 加载显示 GIF 与性能问题](http://www.cnblogs.com/silence-cnblogs/p/6682867.html)

你可能感兴趣的:(iOS加载gif图片)