简体   繁体   English

重新加载View Controller并以编程方式更改UIImageView约束

[英]Reload View Controller and programatically change UIImageView constraint

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. 我有一个UIImageView,它的高度受用户拍摄或从其图库中选择的照片的高宽比影响。 I am having trouble getting the UIImageView to resize appropriately (or at all) after the user selects a photo. 用户选择照片后,我无法让UIImageView适当地(或根本没有)调整大小。

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. 目前在情节提要板中设置了约束,但我想以编程方式覆盖imageView的约束。

You should update the constraints of the UIImageView rather than setting the frame. 您应该更新UIImageView的约束,而不是设置框架。 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." 为图像视图设置一个高度约束,然后为该约束创建一个IBOutlet(控件从该约束拖动到其视图控制器中),将其称为“ imageViewHeightConstraint”。 Then in your code say something like: 然后在您的代码中说出类似以下内容:

imageViewHeightConstraint.constant = multiplyNum*img.size.height

@beyowulf's answer will work and is simple to achieve @beyowulf的答案将起作用并且很容易实现

But if you don't want to setup a constraint outlet, you can specify the ratio attribute in code, and witch is also very simple: 但是,如果您不想设置约束出口,则可以在代码中指定ratio属性,而且witch也非常简单:

//remove the last image ration constraint
if let ratioConstrain = self.ratioConstrain {
    self.imageView.removeConstraint(ratioConstraint)
}

self.ratioConstraint = NSLayoutConstraint(
    item: self.imageView, attribute: NSLayoutAttribute.Width,
    relatedBy: NSLayoutRelation.Equal,
    toItem: self.imageView, 
    attribute: NSLayoutAttribute.Height,
    multiplier: image!.size.width / image!.size.height, 
    constant: 0)

self.imageView.addConstraint(self.ratioConstraint!)

Don't user frame that will not work In this case, you need to remove the height constraint of your imageView, and set the width equal to the super view, you will get what you want: equal width of the screen, and height change with the ratio of the image itself. 不要使用用户框架,在这种情况下,您需要删除imageView的高度约束,并将宽度设置为与超级视图相等,您将获得所需的内容:屏幕宽度相等,高度变化与图像本身的比例。

There's one issue, if the image you get from your roll is too small, you may want the width to be smaller than the screen, in this case you should set the imageView width constraint to lessThanOrEqual(<=), and add some position constraint(like center vertical, center horizontal), that will looks better. 有一个问题,如果从滚动中获取的图像太小,则可能希望宽度小于屏幕,在这种情况下,应将imageView宽度约束设置为lessThanOrEqual(<=),并添加一些位置约束(例如,垂直居中,水平居中),效果会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM