デフォルトの 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;
}