以编程方式将图片裁剪为正方形的解决方案出现问题

Trouble with solution for programmatically cropping a picture to a square

我正在尝试编写代码来制作它,以便相机仅从我在 iPad 上的应用程序拍摄方形照片。我在这里找到了一个解决方案,我从 Objective-C 翻译成 Swift,但是在我用代码拍照后,"Use Photo" 和 "Retake" 按钮不会响应除非我将 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)
}

您使用以下行在整个选择器之上放置了一个叠加层:

  let overlayIV = UIImageView(frame: f)

因为 f 基本上是选择器边界。旋转 "fixes" 的原因是因为视图保持这些边界并且不调整大小(你没有使用任何自动布局),你很幸运按钮在这些边界之外。

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

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

您将需要针对按钮进行调整(因此您不在最前面)。给 overlayIV 一个背景颜色可能是个好主意,这样你就可以在调试过程中看到它。

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

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

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

picker.cameraOverlayView?.addSubview(overlayIV)

我需要

picker.cameraOverlayView = overlayIV

这解决了问题。