我使用默认的 AVCaptureSession 来捕获相机视图。一切正常。但它会运行几秒钟并在调试器中记录消息内存警告并继续工作几秒钟,然后应用程序终止。这可能是我实现的代码中的内存泄漏。但我不知道为什么会发生这种情况,除此之外,我在行中收到警告(将“ViewController *const_strong'发送到不兼容类型'id'的参数
[outputsetSampleBufferDelegate:selfqueue:queue];
..................................................... .......我实现的代码,
-(void)setupCaptureSession
{
NSError *error=nil;
AVCaptureSession *session =[[AVCaptureSessionalloc]init];
self.captureSession=session;
self.captureSession.sessionPreset=AVCaptureSessionPresetMedium;
AVCaptureDevice *device =[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *Input=[AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:&error];
if(!Input){
}
[[selfcaptureSession] addInput:Input];
AVCaptureVideoDataOutput *output =[[AVCaptureVideoDataOutputalloc]init];
[[selfcaptureSession]addOutput:output];
dispatch_queue_t queue =dispatch_queue_create("myQueue", NULL);
[outputsetSampleBufferDelegate:selfqueue:queue];
dispatch_release(queue);
output.videoSettings =[NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithUnsignedInt:kCVPixelFormatType_32BGRA],(id)kCVPixelBufferPixelFormatTypeKey,nil];
AVCaptureConnection *connection =[output connectionWithMediaType:AVMediaTypeVideo];
[connectionsetVideoMinFrameDuration:CMTimeMake(1,15)];
[sessionstartRunning];
[selfsetSession:session];
}
-(UIImage *)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer
{
CVImageBufferRefimageBuffer =CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
void *baseAddress =CVPixelBufferGetBaseAddress(imageBuffer);
size_tbytesPerRow =CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width =CVPixelBufferGetWidth(imageBuffer);
size_t height=CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRefcolorSpace =CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(baseAddress,width,height,8,bytesPerRow,colorSpace,kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
CGImageRefquartzImage =CGBitmapContextCreateImage(context);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *image =[UIImageimageWithCGImage:quartzImage];
CGImageRelease(quartzImage);
return image;
}
////////////////////
-(void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBufferfromConnection:(AVCaptureConnection *)connection{
UIImage *image = [selfimageFromSampleBuffer:sampleBuffer];
//Here i do my processing with image and display a count, Counter updates for few seconds and
Log memory warning and then works again, Then application get terminates
}
-(void)setSession:(AVCaptureSession *)session
{
self.captureSession=session;
}