美文网首页
iOS开发 多线程的使用场景

iOS开发 多线程的使用场景

作者: LuckyBugGo | 来源:发表于2019-08-01 14:08 被阅读0次

1. 生成本地视频的缩略图

图片.png

类似聊天页的小视频和本地视频列表这样的tableView在显示视频的缩略图时,需要使用多线程来解决tableView的卡顿问题。因为对视频进行切图的操作是耗时的,如果在主线程进行,则会卡住tableView的滑动。

// 启动异步线程来对视频进行缩略图操作
dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIImage *shotImage;
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];
        AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
        gen.appliesPreferredTrackTransform = YES;
        CMTime time = CMTimeMakeWithSeconds(0.0, 600);
        
        NSError *error = nil;
        CMTime actualTime;
        CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
        shotImage = [[UIImage alloc] initWithCGImage:image];
        
        CGImageRelease(image);
        // 跳回主线程进行图像更新
        dispatch_async(dispatch_get_main_queue(), ^{
            weakSelf.imageIv.image = shotImage;
            completedBlock(shotImage);
        });
        
    });

相关文章

网友评论

      本文标题:iOS开发 多线程的使用场景

      本文链接:https://www.haomeiwen.com/subject/ugmqdctx.html