iOS 播放 gif 动画

dancer.gif

使用UIWebView播放

- (void)showGifByWebview {
    #pragma clang diagnostic ignored "-Wnonnull"
    NSString *path = [[NSBundle mainBundle] pathForResource:@"dancer" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(60, 90, 100, 75)];
    webview.scalesPageToFit = YES;
    [webview loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    webview.opaque = NO;
    [self.view addSubview:webview];
    [self showGifByImageView];
}

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

- (void)showGifByImageView {
    NSURL *gifUrl = [[NSBundle mainBundle] URLForResource:@"dancer" withExtension:@"gif"];
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)gifUrl, NULL);
    size_t imageCount = CGImageSourceGetCount(gifSource);
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
    for (size_t i = 0; i < imageCount; i++) {
        //获取源图片
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
        UIImage *image = [UIImage imageWithCGImage:imageRef];
        [mutableArray addObject:image];
        CGImageRelease(imageRef);
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 185, 100, 75)];
    imageView.animationImages = mutableArray;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.animationDuration = 0.5;
    [imageView startAnimating];
    [self.view addSubview:imageView];
}

推荐阅读更多精彩内容