Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift 2 open camera in square mode only

So far I have the camera working fine, it opens up in the regular format. But I want the only option to be square format.

Here is my code so far.

@IBAction func takePhoto(sender: AnyObject) {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.sourceType = .Camera

    presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    dismissViewControllerAnimated(true, completion: nil)
}

1 Answers

I don't think you can open camera in square format using UIImagePickerController. What you can do is set UIImagePickerController allowEditing to YES and from the result dict use the key UIImagePickerControllerEditedImage then you will have the squared image so you code becomes .

@IBAction func takePhoto(sender: AnyObject) {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.sourceType = .Camera
    picker.allowsEditing = true

    presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imageView.image = info[UIImagePickerControllerEditedImage] as? UIImage
    dismissViewControllerAnimated(true, completion: nil)
}

Another way around is mentioned in this answer . You can use the logic from answer to write the code in swift.

But if you want to look in details and try out doing some more have a look into

  • AVFoundation,
  • AVCaptureConnection,
  • AVCaptureDevice
  • AVCaptureDeviceInput
  • AVCaptureSession
  • AVCaptureStillImageOutput
  • AVCaptureVideoDataOutput
  • AVCaptureVideoPreviewLayer
  • CoreImage

Also look at apple sample.

like image 70
Imran Avatar answered Dec 08 '25 13:12

Imran