![](/img/trans.png)
[英]Unable to generate video thumbnail from firebase video url in iOS swift?
[英]Unable to generate thumbnail from video url
我正在使用此代碼從視頻網址生成縮略圖
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:videoUrl options:nil];
AVAssetImageGenerator *generator =
[[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef imgRef, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"Couldn't generate Thumbnail, error:%@", error);
}
UIImage *thumbnailImage = [UIImage imageWithCGImage:imgRef];
};
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
但我收到此錯誤。
Couldn't generate Thumbnail, error:Error Domain=AVFoundationErrorDomain
Code=-11800 "The operation could not be completed" UserInfo=
{NSUnderlyingError=0x174251c40 {Error Domain=NSOSStatusErrorDomain Code=-308
"(null)"}, NSLocalizedFailureReason=An unknown error occurred (-308),
NSLocalizedDescription=The operation could not be completed}
我這樣做對嗎? 有人可以幫我嗎? 謝謝
import AVFoundation framework
然后,在 .h 中導入如下
#import <AVFoundation/AVFoundation.h>
並在您想從視頻生成縮略圖時寫下面的代碼
AVURLAsset *assetObj = [[AVURLAsset alloc] initWithURL:self.urlForConevW options:nil];
AVAssetImageGenerator *ImgObj = [[AVAssetImageGenerator alloc] initWithAsset:assetObj];
NSError *error = NULL;
CMTime time = CMTimeMake(1, 65);
CGImageRef refImg = [ImgObj copyCGImageAtTime:time actualTime:NULL error:&error];
NSLog(@"error==%@, Refimage==%@", error, refImg);
UIImage *finalImage= [[UIImage alloc] initWithCGImage:refImg];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.