我正在尝试从视频中获取缩略图,当tableview重复使用单元格时,表格视图滚动粘贴在从视频生成图像时我到目前为止使用此代码尝试了
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIActivityIndicatorView *indicatorG = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
dispatch_async( dispatch_get_main_queue(), ^{
[indicatorG removeFromSuperview];
[indicatorG startAnimating];
[indicatorG setCenter:Cell.thumbnail.center];
[Cell.thumbnail addSubview:indicatorG];
});
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
float getDuration = CMTimeGetSeconds(asset.duration);
CMTime thumbTime = CMTimeMakeWithSeconds(0,1);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}
else{
UIImageView *imgV;
imgV = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:im]];
dispatch_async( dispatch_get_main_queue(), ^{
[indicatorG stopAnimating];
Cell.thumbnail.image = imgV.image;
});
}
// [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
};
CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
});
但是tableview滚动的问题是使用dispatch async来解决的,新问题是缩略图的顺序被这段代码打扰了,有人请帮忙吗?
修改:
网址代码:
NSString *strUrl = [NSString stringWithFormat:@"%@",url];
NSURL *url = [NSURL URLWithString:strUrl];
答案 0 :(得分:0)
如果在滚动表视图时此代码在cellForItemAtIndexPath中连续运行,则这不是一个好的实现。在cellForItemAtIndexPath中从视频生成图像的要求非常昂贵。
在将数据加载到表视图之前,准备并存储缩略图,而不是在cellForItemAtIndexPath中准备缩略图。或者至少在某个地方存储缩略图一次在cellForItemAtIndexPath中处理,同时滚动以便重复使用。比下次在cellForItemAtIndexPath中使用存储的缩略图并始终更新单元格的图像。