我想从视频中获取缩略图,表视图滚动时从视频中生成图像时tableview重用单元格我曾尝试过到目前为止,此代码从视频获取视频缩略图需要时间和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滚动的问题,新的问题是被干扰与此代码的缩略图的顺序,有人请帮助?
编辑:
地址代码:
NSString *strUrl = [NSString stringWithFormat:@"%@",url];
NSURL *url = [NSURL URLWithString:strUrl];
让块保留单元格的indexPath。这样你的订单就不会搞砸了。 – Mercurial
@Mercurial我是iOS新手,你能详细解释一下吗? – John
你的网址对于每个表格行是不同的? – 3stud1ant3