Swift iOS AVVideoCapturePreviewLayer resolves to nil -


in viewdidload() section of code have

    let previewlayer = avcapturevideopreviewlayer(session: capturesession)     previewview.layer.addsublayer(previewlayer) 

however when called in here resolves nil in compiler:

func captureoutput(captureoutput: avcaptureoutput!,didoutputsamplebuffer samplebuffer: cmsamplebuffer!,fromconnection connection: avcaptureconnection!) {     autoreleasepool {         let imagebuffer = cmsamplebuffergetimagebuffer(samplebuffer)!          let formatdescription = cmsamplebuffergetformatdescription(samplebuffer)!         self.currentvideodimensions = cmvideoformatdescriptiongetdimensions(formatdescription)         self.currentsampletime = cmsamplebuffergetoutputpresentationtimestamp(samplebuffer)           // cvpixelbufferlockbaseaddress(imagebuffer, 0)         // let width = cvpixelbuffergetwidthofplane(imagebuffer, 0)         // let height = cvpixelbuffergetheightofplane(imagebuffer, 0)         // let bytesperrow = cvpixelbuffergetbytesperrowofplane(imagebuffer, 0)         // let lumabuffer = cvpixelbuffergetbaseaddressofplane(imagebuffer, 0)         //         // let graycolorspace = cgcolorspacecreatedevicegray()         // let context = cgbitmapcontextcreate(lumabuffer, width, height, 8, bytesperrow, graycolorspace, cgbitmapinfo.allzeros)         // let cgimage = cgbitmapcontextcreateimage(context)         var outputimage = ciimage(cvpixelbuffer: imagebuffer)          if self.filter != nil {              self.filter.setvalue(outputimage, forkey: kciinputimagekey)             outputimage = self.filter.outputimage!         }         let orientation = uidevice.currentdevice().orientation         var t: cgaffinetransform!         if orientation == uideviceorientation.portrait {             t = cgaffinetransformmakerotation(cgfloat(-m_pi / 2.0))         } else if orientation == uideviceorientation.portraitupsidedown {             t = cgaffinetransformmakerotation(cgfloat(m_pi / 2.0))         } else if (orientation == uideviceorientation.landscaperight) {             t = cgaffinetransformmakerotation(cgfloat(m_pi))         } else {             t = cgaffinetransformmakerotation(0)         }         outputimage = outputimage.imagebyapplyingtransform(t)          let cgimage = self.context.createcgimage(outputimage, fromrect: outputimage.extent)         self.ciimage = outputimage          dispatch_sync(dispatch_get_main_queue(), {               self.previewlayer.contents = cgimage           })     } } 

does have idea why? suspicion previewlayer created before capturesession instantiated, placed capturesession.startrunning code in viewdidappear() function , still caused nil value. have in viewdidload section now. if appreciated.

you're not assigning previewlayer property.

let previewlayer = avcapturevideopreviewlayer(session: capturesession) previewview.layer.addsublayer(previewlayer) 

is supposed

previewlayer = avcapturevideopreviewlayer(session: capturesession) previewview.layer.addsublayer(previewlayer) 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -