前置摄像头图片的奇怪问题

时间:2016-08-20 00:36:53

标签: ios swift camera uiimagepickercontroller front-camera

我试图获取软件,以便在拍摄照片时,只有在相机覆盖中看到的正方形内的照片部分显示为照片。后置摄像头工作正常,但是当我尝试使用前置摄像头拍照时,由于某种原因,软件会缩小图像,这就没有意义。关于我可能做错的任何想法都将不胜感激。

@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)
}

请注意,前置摄像头使用与后置摄像头相同的代码。我在调试期间改变了它,因为我认为转换可能不同。它有点像转换被应用两次,但我检查它,它只被应用一次。

感谢您的帮助,
肖恩

1 个答案:

答案 0 :(得分:0)

事实证明,我可以用较小的数字得到我想要的结果。前置摄像头必须具有与后置摄像头不同的像素数。无论如何,如果有人试图这样做,请尝试较小的数字而不是较大的数字。