Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zoom in and zoom out camera on pinch gesture swift

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.

like image 560
Naqeeb Ahmed Avatar asked Jul 01 '19 11:07

Naqeeb Ahmed


People also ask

Can you zoom in by pinching?

Pinch-to-zoom is when a user uses two fingers and pinches to zoom in or out on their mobile device. While it wasn't present on the original touchscreen phones, pinch-to-zoom is now a nearly universal motion for adjusting size on touchscreen devices.

Can you zoom in on camera with fingers?

Pinch 2 or more fingers together or apart to adjust zoom. To zoom temporarily, quickly tap the screen 3 times and hold down your finger on the third tap. Drag your finger to move around the screen. Lift your finger to zoom out.

How do you change the scale of the Uiview with a pinch gesture Swift?

Say, if the user moves his two fingers in a pinch gesture only in a single direction (horizontal), only the width of the uiview should increase/decrease and if the fingers are moved only vertically, the height should change. If the fingers move diagonally, then both height and width of uiview should increase/decrease.


1 Answers

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

like image 67
Naqeeb Ahmed Avatar answered Oct 06 '22 01:10

Naqeeb Ahmed