Change UIImageView size to match image with AutoLayout

后端 未结 2 1393
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 17:54

I want to change my UIImageView height/width to match the image i choose from photo album and i found a snippet on here that does that.

-(CGRect)frameForImag         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 18:09

    Here is a Swift class that will help return the correct sizing for an image based on a width or height limit that you choose:

    class func returnImageSizeForHeightLimit(heightLimit:CGFloat?, orWidthLimit widthLimit:CGFloat?, forImage theImage:UIImage)->CGSize?{
    
        if let myHeightLimit = heightLimit {
    
            let theWidth = (myHeightLimit/theImage.size.height) * theImage.size.width
    
            return CGSizeMake(theWidth, myHeightLimit)
        }
    
        if let myWidthLimit = widthLimit {
    
            let theHeight = (myWidthLimit/theImage.size.width) * theImage.size.height
    
            return CGSizeMake(myWidthLimit, theHeight)
        }
    
        return nil
    }
    

提交回复
热议问题