8

I am using front camera in my app. I want that while taking photos user can zoom in and out camera

I tried this code

let device = AVCaptureDevice.default(for: .video)
print(sender.scale)
let vZoomFactor = sender.scale * prevZoomFactor
if sender.state == .ended {
    prevZoomFactor = vZoomFactor >= 1 ? vZoomFactor : 1
}

if sender.state == .changed{
    do {
        try device!.lockForConfiguration()
        if (vZoomFactor <= device!.activeFormat.videoMaxZoomFactor) {
            device!.videoZoomFactor = max(1.0, min(vZoomFactor, device!.activeFormat.videoMaxZoomFactor))
            device?.unlockForConfiguration()
        } else {
            print("Unable to set videoZoom: (max \(device!.activeFormat.videoMaxZoomFactor), asked \(vZoomFactor))")
        }
    } catch {
        print("\(error.localizedDescription)")
    }
}

every thing is working fine in back camera but zoom is not applying on front camera.

fdelafuente
  • 1,114
  • 1
  • 13
  • 24
Naqeeb
  • 1,121
  • 8
  • 25
  • I feel, front cameras don't support zoom in/out feature. Did you try iPhone's camera app? – Kamran Jul 01 '19 at 12:06
  • yes i checked some apps like https://apps.apple.com/us/app/mirror/id431471943 , it support zoom in front camera – Naqeeb Jul 01 '19 at 12:20
  • Ok, did you debug zoom factor values in different modes? What is failing the if condition? – Kamran Jul 01 '19 at 12:26
  • yes i did nothing is failing .. in-fact when i switch to back cam it working fine when i switch to front code is working but zoom not applying – Naqeeb Jul 01 '19 at 12:34

1 Answers1

7

well, after spending hours on this code i got the there where i was making the mistake.

let device = AVCaptureDevice.default(for: .video)

this will by default get the back camera and work perfect but when i switch it to front it is till considering it as back camera , so i just added a condition

    if  currentcam == frontcam {
      let device = frontcam
      //did other stuff for zooimng
     } 

   else {
     let device = AVCaptureDevice.default(for: .video)
     //did other stuff for zooimng
  }

this worked fine for me

Naqeeb
  • 1,121
  • 8
  • 25