解决方案有问题,以编程方式将图片裁剪为正方形

时间:2016-07-19 18:24:37

标签: ios swift uiimagepickercontroller

我正在尝试编写代码,以便相机只在我的应用程序iPad上拍摄方形照片。我在这里找到了一个解决方案,我将其从Objective-C转换为Swift,但在我用代码拍照后,“使用照片”和“重新拍摄”按钮将不会响应触摸,除非我将iPad旋转90度并且从横向模式点击按钮。这当然是非常不受欢迎的。关于如何修复我的代码的任何提示都会非常棒。

@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
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .Camera
        var f: CGRect
        f = picker.view.bounds
        //f.size.width -= picker.navigationBar.bounds.size.width
        f.size.height -= picker.navigationBar.bounds.size.height
        var barHeight: CGFloat
        barHeight = (f.size.height - f.size.width) / 2
        UIGraphicsBeginImageContext(f.size)
        var edgeColor: UIColor
        edgeColor = UIColor(white: 0, alpha: 0.5)
        edgeColor.set()
        UIRectFillUsingBlendMode(CGRectMake(0, 0, f.size.width, barHeight), CGBlendMode.Normal)
        UIRectFillUsingBlendMode(CGRectMake(0, f.size.height - barHeight, f.size.width, barHeight), CGBlendMode.Normal)
        var overlayImage: UIImage
        overlayImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        let overlayIV = UIImageView(frame: f)
        overlayIV.image = overlayImage
        picker.cameraOverlayView?.addSubview(overlayIV)
        self.presentViewController(picker, animated: true, completion: nil)
    }

    let chooseExistingPicture = UIAlertAction(title: "Use Existing", style: UIAlertActionStyle.Default) {(alertAction: UIAlertAction!) -> Void in

        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
    let width = Double((imageSize?.width)!)
    let height = Double((imageSize?.height)!)
    if width != height {
        let newDimension = min(width, height)
        let widthOffset = (width - newDimension) / 2
        let heightOffset = (height - newDimension) / 2
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGFloat(newDimension), CGFloat(newDimension)), false, 0.0)
        image?.drawAtPoint(CGPointMake(CGFloat(-widthOffset), CGFloat(-heightOffset)))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    CustomPicture.image = image as? UIImage;
    dismissViewControllerAnimated(true, completion: nil)
}

2 个答案:

答案 0 :(得分:0)

你用这一行在整个选择器的顶部放置了一个叠加层:

  let overlayIV = UIImageView(frame: f)

因为f基本上是选择器界限。旋转“修复”它的原因是因为视图保留了这些边界并且没有调整大小(你没有使用任何自动布局),而且你很幸运,按钮超出了那些边界。

因此,以同样的方式调整导航栏的f高度:

 f.size.height -= picker.navigationBar.bounds.size.height

您需要调整按钮(因此您不在顶部)。为overlayIV提供背景色可能是个好主意,这样你就可以在调试过程中看到它。

此外,您可以使用视图层次结构调试器:https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html

最后,考虑设置一些布局约束,以便在旋转时自行调整大小。

答案 1 :(得分:0)

我发现我的代码存在问题,而不是

picker.cameraOverlayView?.addSubview(overlayIV)

我需要

picker.cameraOverlayView = overlayIV

这解决了这个问题。