4
我試圖從Swift中的視頻中提取幀作爲UIImages。我發現了幾個Objective C解決方案,但我在Swift中找不到任何東西。假設以下內容是正確的,那麼有人可以幫助我將以下內容轉換爲Swift,或者讓我自己承擔如何做到這一點?迅速從視頻中提取幀
- (UIImage *)imageFromVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
// courtesy of memmons
// http://stackoverflow.com/questions/1518668/grabbing-the-first-frame-of-a-video-from-uiimagepickercontroller
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetIG =
[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetIG.appliesPreferredTrackTransform = YES;
assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *igError = nil;
thumbnailImageRef =
[assetIG copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)
actualTime:NULL
error:&igError];
if (!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@", igError);
UIImage *image = thumbnailImageRef
? [[UIImage alloc] initWithCGImage:thumbnailImageRef]
: nil;
return image;
}
我從來沒有爲自己試過這個,但我希望[this](https://objectivec2swift.com/)能幫助你 – UsamaMan