Sean Zlatnik
Sean Zlatnik

Reputation: 309

Trouble with solution for programmatically cropping a picture to a square

I'm trying to write code to make it so that the camera takes square pictures only from my app on an iPad. I found a solution on here which I translated from Objective-C to Swift, but after I take a picture with the code, the "Use Photo" and "Retake" buttons won't respond to touch unless I rotate the iPad 90 degrees and hit the button from landscape mode. This is of course quite undesirable. Any hints on how to fix my code would be fantastic.

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

Upvotes: 0

Views: 392

Answers (2)

Sean Zlatnik
Sean Zlatnik

Reputation: 309

I found the problem with my code instead of

picker.cameraOverlayView?.addSubview(overlayIV)

I needed to have

picker.cameraOverlayView = overlayIV

This solves the problem.

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89172

You put an overlay on top of the whole picker with this line:

  let overlayIV = UIImageView(frame: f)

since f is basically the picker bounds. The reason why rotating "fixes" it is because the view keeps these bounds and doesn't resize (you didn't use any auto-layout), and you are just lucky that the buttons are outside of those bounds.

So, in the same way you adjusted f's height for the navigation bar with:

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

You are going to need to adjust for the buttons (so you are not on top). It might be a good idea to give overlayIV a background color so you can see it during debugging.

Also, you could use the view hierarchy debugger: https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html

Finally, consider setting up some layout constraints so that it resizes itself when you rotate.

Upvotes: 0

Related Questions