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

类似聊天页的小视频和本地视频列表这样的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);
});
});
网友评论