iphone - 如何从应用程序目录中的视频中抓取单帧?

标签 iphone ios cocoa-touch

如何使用 UIImageView 来显示 .mov 文件的可见框架,即在例如视频中可见的预览框架?取景器或任何 youtube 视频等。

我希望能够从保存到应用程序库目录的 .mov 文件中获取预览帧,因此它们将不是 AVAssets。

最佳答案

AVFoundation 中的 AVAssetImageGenerator 可用于加载相册和本地应用程序目录中的视频。

这是一个辅助方法,可以在任何给定的时间间隔从视频 URL(应用程序内部或外部)返回图像:

+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = time;
    NSError *thumbnailImageGenerationError = nil;
    thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];

    NSAssert(thumbnailImageRef, @"CGImageRef shall never be nil.");

    UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;

    return thumbnailImage;
}

关于iphone - 如何从应用程序目录中的视频中抓取单帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18065800/

相关文章:

iphone - 使网站兼容 iPhone

iPhone 5 隐藏在 View 中的栏的布局

iphone - UIToolBar 意外地在 UIBarButtonItem 实例上注册点击,即使在距它们相当远的地方点击也是如此

iPhone(SDK 2.2): adusting playback volume while NOT actively playing music w/AVFoundation?

iphone - UIView 始终以纵向模式加载

ios - swift : Disable FrontViewController when Rear is Displayed

iphone - 调用 PopToRootViewController 后如何清理 View Controller ?

ios - 使 UIButton 背景图像大于框架

ios - 匿名闭包参数不能在具有显式参数的闭包内使用

objective-c - 在 UITextView 中移动光标