[英]Extract thumbnail from video url in threads
我在另一個線程中使用(Grand Central Dispatch)在我的MPMoviePlayer上遇到問題。 我想通過網址吸引視頻,剪切第5幀並返回圖片。 在模擬器中工作正常,但在真實設備上 - 下降。 這有什么問題? 也許它只適用於主線程?
NSURL* url = [NSURL URLWithString:filePath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void)
{
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.shouldAutoplay = NO;
UIImage* thumbnail = [moviePlayer thumbnailImageAtTime:5 timeOption:MPMovieTimeOptionNearestKeyFrame];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[[Singleton sharedSingleton].imageCache setValue:thumbnail forKey:filePath];
[cell.presentationViewOutlet setImage:thumbnail];;
});
});
現在,我嘗試使用另一種方法,但它也 - 在模擬器上運行良好,並且不顯示真實設備。 有時返回imageRef = (null)
,有時imageRef = <CGImage 0x1766dd20>
。
編輯:
NSURL* url = [NSURL URLWithString:filePath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void)
{
AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = TRUE;
CMTime time = CMTimeMakeWithSeconds(1, 1);
NSError *err = NULL;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&err];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
NSLog(@"imageRef = %@", imageRef);
UIImage* image = [UIImage imageWithCGImage:imageRef];
if (image != nil)
{
NSLog(@"image != nil");
[view setImage:image];
NSString* videoURLString = [videoURL absoluteString];
[[Singleton sharedSingleton].imageCache setValue:image forKey:videoURLString];
CGImageRelease(imageRef);
}
else
{
NSLog(@"err = %@", err);
}
});
});
錯誤:
如果imageRef =(null)show err = Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x156c1a50 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x155b29a0 "The operation couldn't be completed. (OSStatus error -12792.)", NSLocalizedFailureReason=An unknown error occurred (-12792)}
MPMoviePlayerController是用戶界面控制器。 因此,它幾乎肯定不是線程安全的。 文檔沒有具體說明,但我敢打賭金錢不是。
如果在異步部分中移動imageref,它會更好嗎? 喜歡:
NSURL* url = [NSURL URLWithString:filePath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void)
{
AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = TRUE;
CMTime time = CMTimeMakeWithSeconds(1, 1);
dispatch_async(dispatch_get_main_queue(), ^(void)
{
NSError *err = NULL;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&err];
NSLog(@"imageRef = %@", imageRef);
UIImage* image = [UIImage imageWithCGImage:imageRef];
if (image != nil)
{
NSLog(@"image != nil");
[view setImage:image];
NSString* videoURLString = [videoURL absoluteString];
[[Singleton sharedSingleton].imageCache setValue:image forKey:videoURLString];
CGImageRelease(imageRef);
}
else
{
NSLog(@"err = %@", err);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.