iOS加载gif图片

2017-05-16  本文已影响113人  薄凉_简书

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版本)

    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

上一篇 下一篇

猜你喜欢

热点阅读