I have a UIImageView which its height is affected by the aspect ratio of a photo that the user either takes or selects from their library. I am having trouble getting the UIImageView to resize appropriately (or at all) after the user selects a photo.
Code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//let mediaType2 : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let mediaType : CFString = info[UIImagePickerControllerMediaType] as! CFString
if mediaType == kUTTypeMovie {
let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path!) {
UISaveVideoAtPathToSavedPhotosAlbum(path!, self, "video:didFinishSavingWithError:contextInfo:", nil)
}
}
else{
let img : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let screenSize: CGRect = UIScreen.mainScreen().bounds
let multiplyNum = screenSize.width / img.size.width
imageView.frame = CGRectMake(0, 0, screenSize.width, multiplyNum*img.size.height)
imageView.image = img
}
self.dismissViewControllerAnimated(true, completion: {})
}
The constraints are currently set up inside the storyboard but I want to programatically override imageView's constraint.
You should update the constraints of the UIImageView rather than setting the frame. Set a height constraint for the image view then create an IBOutlet for that constraint (control drag from the constraint into it's view controller), call it something like "imageViewHeightConstraint." Then in your code say something like:
imageViewHeightConstraint.constant = multiplyNum*img.size.height