我正在尝试为此Apple Q& A代码提供有关如何捕获视频帧的IOS 7代码:AppleQ&A1702
到目前为止,我认为我已经使用is code:
正确替换了已弃用的minFrameDuration// If you wish to cap the frame rate to a known value, such as 2 fps, set minFrameDuration.
//output.minFrameDuration = CMTimeMake(1, 2); //deprecated
[device setActiveVideoMinFrameDuration:CMTimeMake(1, 2)]; //new in IOS7
[device setActiveVideoMaxFrameDuration:CMTimeMake(1, 2)];
但是,我无法运行代码,因为我收到了“No interface”错误'对于此行的setSession:
// Assign session to an ivar.
[self setSession:session];
此外,我在此行收到了不兼容的类型警告:
[output setSampleBufferDelegate:self queue:queue];
我相信这个错误和警告有简单的答案,但经过大量的搜索,我感到难过。 有人可以帮我弄清楚为什么我直接从Apple示例代码中得到此错误和警告?谢谢卡门
[EDIT1]我不认为我已经完全放弃了我已弃用的minFrameDuration修复程序,而且可能不需要setSession方法。我正在尝试拼凑剪切和粘贴代码,以便以指定的帧速率从视频中捕获图像,而IOS7应用程序不需要在低于7.0的情况下运行。我暂时将捕获的图像放在我的故事板上的UIImageView中。
不应该这么难。
这是我到目前为止的整个代码部分,但是当我从viewDidLoad调用setupCaptureSession并且上面提到的不兼容类型警告仍然存在后仍然没有触发captureOutput方法,任何人都可以看到错误(我仍然是新手所以很好:)
// Create and configure a capture session and start it running
- (void)setupCaptureSession
{
NSError *error = nil;
// Create the session
AVCaptureSession *session = [[AVCaptureSession alloc] init];
// Configure the session to produce resolution for video frames
session.sessionPreset = AVCaptureSessionPresetHigh;
// Find a suitable AVCaptureDevice
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Create a device input with the device and add it to the session.
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (!input) {
// Handling the error appropriately.
}
[session addInput:input];
// Create a VideoDataOutput and add it to the session
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
// Configure your output.
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[output setSampleBufferDelegate:self queue:queue];
// Specify the pixel format
output.videoSettings =
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(id)kCVPixelBufferPixelFormatTypeKey];
// If you wish to cap the frame rate to a known value, such as 2 fps, set
// minFrameDuration.
//output.minFrameDuration = CMTimeMake(1, 2); //deprecated
//AVCaptureConnection *conn = [output connectionWithMediaType:AVMediaTypeVideo];
//if (conn.supportsVideoMinFrameDuration) // these are deprecated in IOS7
// conn.videoMinFrameDuration = CMTimeMake(1,2);
//if (conn.supportsVideoMaxFrameDuration)
// conn.videoMaxFrameDuration = CMTimeMake(1,2);
NSError *error2;
[device lockForConfiguration:&error2];
if (error2 == nil) {
if (device.activeFormat.videoSupportedFrameRateRanges){
[device setActiveVideoMinFrameDuration:CMTimeMake(1, 2)];
[device setActiveVideoMaxFrameDuration:CMTimeMake(1, 2)];
}else{
//handle condition
}
}else{
// handle error2
}
[device unlockForConfiguration];
// Start the session running to start the flow of data
[session startRunning];
// Assign session to an ivar.
//[self setSession:session]; //not sure why need this, can't find code that works for it
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
NSLog(@"captureOutput: didOutputSampleBufferFromConnection");
// Create a UIImage from the sample buffer data
UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
dispatch_async(dispatch_get_main_queue(), ^{
//< Add your code here that uses the image >
[self.imageView setImage:image];
[self.view setNeedsDisplay];}
);
}
// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// Get the number of bytes per row for the pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// Free up the context and color space
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage];
// Release the Quartz image
CGImageRelease(quartzImage);
return (image);
}
答案 0 :(得分:0)
因此,这个问题的关键似乎是苹果指令“将会话分配给ivar&#39;然后调用他们没有提供代码的方法。
由于我是新手并且使用ARC,我希望不必了解合成/ ivars,我仍然没有。
但是,这里的代码是基本的捕获代码(我的整个viewController。)。基本上我必须在我的viewController.m
中的导入行之后声明会话也许有人可以解释我的所作所为,但我现在正在考虑解决此问题(仍然不确定问题中提到的不兼容类型警告):
#import "CJKViewController.h"
AVCaptureSession *session;
@interface CJKViewController ()
@end
@implementation CJKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create the session
[self setupCaptureSession];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Create and configure a capture session and start it running
- (void)setupCaptureSession
{
NSError *error = nil;
// AVCaptureSession *session = [[AVCaptureSession alloc] init];
session = [[AVCaptureSession alloc] init];
// Configure the session to produce lower resolution video frames, if your
// processing algorithm can cope. We'll specify medium quality for the
// chosen device.
session.sessionPreset = AVCaptureSessionPresetHigh;
// Find a suitable AVCaptureDevice
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Create a device input with the device and add it to the session.
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (!input) {
// Handling the error appropriately.
}
[session addInput:input];
// Create a VideoDataOutput and add it to the session
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
// Configure your output.
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[output setSampleBufferDelegate:self queue:queue];
// Specify the pixel format
output.videoSettings =
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(id)kCVPixelBufferPixelFormatTypeKey];
// If you wish to cap the frame rate to a known value, such as 2 fps, set
// minFrameDuration.
//output.minFrameDuration = CMTimeMake(1, 2); //deprecated
//AVCaptureConnection *conn = [output connectionWithMediaType:AVMediaTypeVideo];
//if (conn.supportsVideoMinFrameDuration)
// conn.videoMinFrameDuration = CMTimeMake(1,2);
//if (conn.supportsVideoMaxFrameDuration)
// conn.videoMaxFrameDuration = CMTimeMake(1,2);
NSError *error2;
[device lockForConfiguration:&error2];
if (error2 == nil) {
if (device.activeFormat.videoSupportedFrameRateRanges){
[device setActiveVideoMinFrameDuration:CMTimeMake(1, 2)];
[device setActiveVideoMaxFrameDuration:CMTimeMake(1, 2)];
}
}else{
// handle error2
}
[device unlockForConfiguration];
// Start the session running to start the flow of data
[session startRunning];
// Assign session to an ivar.
//[self setSession:session];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
NSLog(@"captureOutput: didOutputSampleBufferFromConnection");
// Create a UIImage from the sample buffer data
UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
dispatch_async(dispatch_get_main_queue(), ^{
//< Add your code here that uses the image >
[self.imageView setImage:image];
[self.view setNeedsDisplay];}
);
}
// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// Get the number of bytes per row for the pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// Free up the context and color space
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage];
// Release the Quartz image
CGImageRelease(quartzImage);
return (image);
}
@end