Search code examples
iosswiftcamerauiimagepickercontrollerfront-camera

Weird issue with Front facing camera pictures


I'm trying to get the software so that when a picture is taken, only the part of the picture within a square that you see in the camera overlay shows up as the picture. The rear camera works fine, but when I try to take pictures with the front camera, for some reason the software shrinks the picture and it makes no sense why. Any ideas about what I might be doing wrong would be appreciated.

@IBAction func takePicture(sender: UIButton)
{
    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    let takePictureAction = UIAlertAction(title: "Take Picture", style: UIAlertActionStyle.Default)
    { (alertAction: UIAlertAction!) -> Void in
        self.isCameraPic = true
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .Camera
        var f: CGRect
        f = picker.view.bounds
        let overlayIV = UIImageView(frame: f)

        overlayIV.image = UIImage(named: "cameraTarget")
        picker.cameraOverlayView = overlayIV
        self.presentViewController(picker, animated: true, completion: nil)
    }
    let chooseExistingPicture = UIAlertAction(title: "Use Existing", style: UIAlertActionStyle.Default) {(alertAction: UIAlertAction!) -> Void in
        self.isCameraPic = false
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .PhotoLibrary
        self.presentViewController(picker, animated: true, completion: nil)
    }
    alertController.addAction(takePictureAction)
    alertController.addAction(chooseExistingPicture)
    alertController.view.tintColor = UIColor.blackColor()
    presentViewController(alertController, animated: true, completion: nil)
    let presentationController = alertController.popoverPresentationController
    presentationController?.sourceView = self.view
    presentationController?.sourceRect = CGRectMake(330, 210, 330, 210)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{

    var image = info[UIImagePickerControllerOriginalImage]

    let imageSize = image?.size
    if isCameraPic! == true && picker.cameraDevice == UIImagePickerControllerCameraDevice.Rear
    {
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGFloat(1140), CGFloat(1140)), false, 0.0)
        image?.drawAtPoint(CGPointMake(CGFloat(-400), CGFloat(-700)))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    else if picker.cameraDevice == UIImagePickerControllerCameraDevice.Front
    {
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGFloat(4000), CGFloat(4000)), true, 0.0)
        image?.drawAtPoint(CGPointMake(CGFloat(0), CGFloat(0)))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

    }
    CustomPicture.image = image as? UIImage;
    hasCustomImage = true
    changedImage = true
    dismissViewControllerAnimated(true, completion: nil)
}

Note that the front facing camera used to use the same code as the rear one. I changed it during debugging, as I thought maybe the transformation was different. It is sort of behaving like the transformation is being applied twice, but i checked it and it is only being applied once.

Thanks for your help,
Sean


Solution

  • It turns out that I can get the result I want with smaller numbers. The front camera must have a different number of pixels than the back camera. Anyhow, if anyone is trying to do this, try smaller numbers rather than larger ones.