ios - How to switch to front camera when i double tap ( SWIFT ) -


at first want apologize approximative english.

i work on project iphone using swift.

in project, have 3 views in 1 of them put camera view (the camera) snapchat menu. when double tap want switch front camera.

i added tap gesture recognizer code don't know how initialize front camera in it.

here's complete code. if need more info tell me! thank you.

    import uikit     import avfoundation       class view2: uiviewcontroller,  uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate{  var capturesession : avcapturesession? var stillimageoutput : avcapturestillimageoutput? var previewlayer : avcapturevideopreviewlayer?   @ibaction func doubletap(sender: uitapgesturerecognizer) {   }   @iboutlet var cameraview: uiview!  override func viewdidload() {     super.viewdidload()      // additional setup after loading view.       }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }   override func viewdidappear(animated: bool) {     super.viewdidappear(animated)     previewlayer?.frame = cameraview.bounds }  override func viewwillappear(animated: bool) {     super.viewwillappear(animated)      capturesession = avcapturesession()     capturesession?.sessionpreset = avcapturesessionpreset1920x1080      //  déclaration caméra frontale             let backcamera = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo)      var error : nserror?     var input: avcapturedeviceinput!     {         input = try avcapturedeviceinput(device: backcamera)     } catch let error1 nserror {         error = error1         input = nil     }      if (error == nil && capturesession?.canaddinput(input) != nil){          capturesession?.addinput(input)          stillimageoutput = avcapturestillimageoutput()         stillimageoutput?.outputsettings = [avvideocodeckey : avvideocodecjpeg]          if (capturesession?.canaddoutput(stillimageoutput) != nil){             capturesession?.addoutput(stillimageoutput)              previewlayer = avcapturevideopreviewlayer(session: capturesession)             previewlayer?.videogravity = avlayervideogravityresizeaspect             previewlayer?.connection.videoorientation = avcapturevideoorientation.portrait             cameraview.layer.addsublayer(previewlayer!)             capturesession?.startrunning()           }       }   } 

may help

- (avcapturedevice *)showfrontcamera {     nsarray *devices = [avcapturedevice deviceswithmediatype:avmediatypevideo];     (avcapturedevice *device in devices) {         if ([device position] == avcapturedevicepositionfront) {             return device;         }     }     return nil; } 

this function return front camera view. when double click call function switch

avcapturedevice *device = [self showfrontcamera]; 

i have code in objective-c, may can convert swift per need.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -