Sean Zlatnik Sean Zlatnik - 1 year ago 93
iOS Question

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
var edgeColor: UIColor
edgeColor = UIColor(white: 0, alpha: 0.5)
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()

let overlayIV = UIImageView(frame: f)
overlayIV.image = overlayImage
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.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()
CustomPicture.image = image as? UIImage;
dismissViewControllerAnimated(true, completion: nil)

Answer Source

I found the problem with my code instead of


I needed to have

picker.cameraOverlayView = overlayIV

This solves the problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download