iOS 播放 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];
}

人已赞赏
iOS文章

iOS 那些你可能会用到的宏定义

2019-10-11 6:15:15

iOS文章

iOS WKWebView UI增强(上拉刷新,JS交互,加载进度条)

2019-10-11 8:44:33

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索