{"id":616,"date":"2012-11-27T09:56:39","date_gmt":"2012-11-27T09:56:39","guid":{"rendered":"http:\/\/www.bardecode.com\/newsite\/?p=616"},"modified":"2013-03-01T14:43:33","modified_gmt":"2013-03-01T14:43:33","slug":"ios-41-code-detail","status":"publish","type":"post","link":"https:\/\/www.bardecode.com\/newsite\/ios-41-code-detail\/","title":{"rendered":"IOS 4.1 code detail"},"content":{"rendered":"<p>Detailed description of work-around for AVCaptureVideoPreviewLayer problem in IOS 4.1<\/p>\n<p>IOS 4.1 seems to contain a critical bug in the AVCaptureVideoPreviewLayer which causes app&#8217;s to freeze when an instance of the class is created.<\/p>\n<p>The normal way to use AVCaptureVideoPreviewLayer is as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Create the session<\/p>\n<p>session = [[AVCaptureSession\u00a0alloc]\u00a0init];<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Configure the session to produce lower resolution video frames, if your<\/p>\n<p>\/\/ processing algorithm can cope. We&#8217;ll specify medium quality for the<\/p>\n<p>\/\/ chosen device.<\/p>\n<p>session.sessionPreset\u00a0=\u00a0AVCaptureSessionPresetMedium;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Find a suitable AVCaptureDevice<\/p>\n<p>AVCaptureDevice\u00a0*device = [AVCaptureDevice\u00a0defaultDeviceWithMediaType:AVMediaTypeVideo];<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Create a device input with the device and add it to the session.<\/p>\n<p>AVCaptureDeviceInput\u00a0*input = [AVCaptureDeviceInput\u00a0deviceInputWithDevice:device<\/p>\n<p>error:&amp;error];<\/p>\n<p>if\u00a0(!input) {<\/p>\n<p>\/\/ Handling the error appropriately.<\/p>\n<p>return\u00a0;<\/p>\n<p>}<\/p>\n<p>[session\u00a0addInput:input];<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Create a VideoDataOutput and add it to the session<\/p>\n<p>AVCaptureVideoDataOutput\u00a0*output = [[[AVCaptureVideoDataOutput\u00a0alloc]\u00a0init]\u00a0autorelease];<\/p>\n<p>[session\u00a0addOutput:output];<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Configure your output.<\/p>\n<p>dispatch_queue_t\u00a0queue =\u00a0dispatch_queue_create(&#8220;myQueue&#8221;,\u00a0NULL);<\/p>\n<p>[output\u00a0setSampleBufferDelegate:self\u00a0queue:queue];<\/p>\n<p>dispatch_release(queue);<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Specify the pixel format<\/p>\n<p>output.videoSettings\u00a0=<\/p>\n<p>[NSDictionary\u00a0dictionaryWithObject:<\/p>\n<p>[NSNumber\u00a0numberWithInt:kCVPixelFormatType_32BGRA]\u00a0forKey:(id)kCVPixelBufferPixelFormatTypeKey];<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ We use 10 fps for barcode reading.<\/p>\n<p>output.minFrameDuration\u00a0=\u00a0CMTimeMake(1,\u00a010);<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Start the session running to start the flow of data<\/p>\n<p>[session\u00a0startRunning];<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Create the video capture preview<\/p>\n<p>\/\/ APP WILL FREEZE ON THIS NEXT LINE:<\/p>\n<p>AVCaptureVideoPreviewLayer\u00a0*previewLayer = [AVCaptureVideoPreviewLayer\u00a0layerWithSession:Session];<\/p>\n<p>previewLayer.frame\u00a0= aView.bounds;\u00a0\/\/ Assume you want the preview layer to fill the view.<\/p>\n<p>[aView.layer\u00a0addSublayer:previewLayer];<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Any app using the above code on IOS 4.1 will freeze at the line indicated.<\/p>\n<p>&nbsp;<\/p>\n<p>The work-around is to delay the assignment of the output device to the session until after the preview layer has been created:<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Create the session<\/p>\n<p>11px\/normal Menlo; padding: 0px; margin: 0px;&#8221;&gt;session = [[AVCaptureSession\u00a0alloc]\u00a0init];<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Configure the session to produce lower resolution video frames, if your<\/p>\n<p>\/\/ processing algorithm can cope. We&#8217;ll specify medium quality for the<\/p>\n<p>\/\/ chosen device.<\/p>\n<p>session.sessionPreset\u00a0=\u00a0AVCaptureSessionPresetMedium;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Find a suitable AVCaptureDevice<\/p>\n<p>AVCaptureDevice\u00a0*device = [AVCaptureDevice\u00a0defaultDeviceWithMediaType:AVMediaTypeVideo];<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Create a device input with the device and add it to the session.<\/p>\n<p>AVCaptureDeviceInput\u00a0*input = [AVCaptureDeviceInput\u00a0deviceInputWithDevice:device<\/p>\n<p>error:&amp;error];<\/p>\n<p>if\u00a0(!input) {<\/p>\n<p>\/\/ Handling the error appropriately.<\/p>\n<p>return\u00a0;<\/p>\n<p>}<\/p>\n<p>[session\u00a0addInput:input];<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Create the video capture preview<\/p>\n<p>AVCaptureVideoPreviewLayer\u00a0*previewLayer = [AVCaptureVideoPreviewLayer\u00a0layerWithSession:Session];<\/p>\n<p>previewLayer.frame\u00a0= aView.bounds;\u00a0\/\/ Assume you want the preview layer to fill the view.<\/p>\n<p>[aView.layer\u00a0addSublayer:previewLayer];<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Create a VideoDataOutput and add it to the session<\/p>\n<p>AVCaptureVideoDataOutput\u00a0*output = [[[AVCaptureVideoDataOutput\u00a0alloc]\u00a0init]\u00a0autorelease];<\/p>\n<p>[session\u00a0addOutput:output];<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Configure your output.<\/p>\n<p>dispatch_queue_t\u00a0queue =\u00a0dispatch_queue_create(&#8220;myQueue&#8221;,\u00a0NULL00;&#8221;&gt;);<\/p>\n<p>[output\u00a0setSampleBufferDelegate:self\u00a0queue:queue];<\/p>\n<p>dispatch_release(queue);<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Specify the pixel format<\/p>\n<p>output.videoSettings\u00a0=<\/p>\n<p>[NSDictionary\u00a0dictionaryWithObject:<\/p>\n<p>[NSNumber\u00a0numberWithInt:kCVPixelFormatType_32BGRA]\u00a0forKey:(id)kCVPixelBufferPixelFormatTypeKey];<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ We use 10 fps for barcode reading.<\/p>\n<p>output.minFrameDuration\u00a0=\u00a0CMTimeMake(1,\u00a010);<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Start the session running to start the flow of data<\/p>\n<p>[session\u00a0startRunning];<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Detailed description of work-around for AVCaptureVideoPreviewLayer problem in IOS 4.1 IOS 4.1 seems to contain a critical bug in the AVCaptureVideoPreviewLayer which causes app&#8217;s to freeze when an instance of the class is created. The normal way to use AVCaptureVideoPreviewLayer is as follows: &nbsp; \/\/ Create the session session = [[AVCaptureSession\u00a0alloc]\u00a0init]; &nbsp; &nbsp; \/\/ Configure &hellip; <\/p>\n","protected":false},"author":1,"featured_media":316,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[139,19,56,58,66,138,134,59,62],"class_list":["post-616","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development-kits","tag-4-1","tag-barcode","tag-barcode-reader","tag-code","tag-development","tag-ios","tag-iphone-2","tag-snippet","tag-software"],"_links":{"self":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/616"}],"collection":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":3,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"predecessor-version":[{"id":1630,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/616\/revisions\/1630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/media\/316"}],"wp:attachment":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}