只要查看加载,我想显示暂停的视频,并在其上显示第一帧。 为此,我希望将视频的第一帧作为UImage显示,直到视频播放为止。
iOS中是否存在通过其URL保存本地视频文件的框架?
更新:请同时考虑我的解决方案和Stavash,并查看适合您的方式。
答案 0 :(得分:2)
我发现了一种运行良好的方法!
查看AVAssetImageGenerator
课程Apple documentation和AV Foundation Programming Guide上的{{3}}。
它有一个名为copyCGImageAtTime:actualTime:error:
使用AVasset
类AVFoundation FrameWork
。
AVasset有一个类方法+ assetWithURL:
,它从文件的URL为您提供资产对象。
此方法仅适用于iOS 5或更高版本,以防万一
我们可以做一个respondsToSelector,如果失败则使用
+ (AVURLAsset *)URLAssetWithURL:(NSURL *)URL options:(NSDictionary *)options
AVURLAsset是AVAsset的具体子类,可在本地视频文件的URL可用时使用。
创建资产后,它会有像naturalSize,duration这样的属性以及许多处理媒体文件的方法。
使用+ assetImageGeneratorWithAsset:
的{{1}}并将资源传递给它并创建一个Imagegenerator。
调用上面的方法获取AVAssetImageGenerator
,然后使用UIImage的imageWithCGImage获取图像并将其设置为UIImageView的图像属性!
简单明了!
注意:不要忘记为CMTime添加Core Media frameWork,您将传递以获取相应的图像帧。
还为所有AVFoundation类添加AVFoundation FrameWork。
答案 1 :(得分:1)
我找到了以下代码,它可能对您有所帮助:
-(void) writeVideoFrameAtTime:(CMTime)time {
if (![videoWriterInput isReadyForMoreMediaData]) {
NSLog(@"Not ready for video data");
}
else {
@synchronized (self) {
UIImage* newFrame = [self.currentScreen retain];
CVPixelBufferRef pixelBuffer = NULL;
CGImageRef cgImage = CGImageCreateCopy([newFrame CGImage]);
CFDataRef image = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
int status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, avAdaptor.pixelBufferPool, &pixelBuffer);
if(status != 0){
//could not get a buffer from the pool
NSLog(@"Error creating pixel buffer: status=%d", status);
}
// set image data into pixel buffer
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
uint8_t* destPixels = CVPixelBufferGetBaseAddress(pixelBuffer);
CFDataGetBytes(image, CFRangeMake(0, CFDataGetLength(image)), destPixels); //XXX: will work if the pixel buffer is contiguous and has the same bytesPerRow as the input data
if(status == 0){
BOOL success = [avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
if (!success)
NSLog(@"Warning: Unable to write buffer to video");
}
//clean up
[newFrame release];
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );
CVPixelBufferRelease( pixelBuffer );
CFRelease(image);
CGImageRelease(cgImage);
}
}
}
取自http://codethink.no-ip.org/wordpress/archives/673#comment-8146