Get width of a resized image after UIViewContentModeScaleAspectFit

前端 未结 6 1644
忘了有多久
忘了有多久 2021-01-30 13:56

I have my UIImageView and I put an image into it that I resize like this:

UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.         


        
6条回答
  •  深忆病人
    2021-01-30 14:31

    For Swift 2, you can use this code snippet to align an aspectFitted image to the bottom of the screen (sampled from earlier answers to this question -> thanks to you, guys)

    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let screenWidth = CGFloat(screenSize.width)
    let screenHeight = CGFloat(screenSize.height)
    
    imageView.frame = CGRectMake(0, screenHeight - (imageView.image?.size.height)! , screenWidth, (imageView.image?.size.height)!)
    
    let newSize:CGSize = getScaledSizeOfImage(imageView.image!, toSize: self.view.frame.size)
    
    imageView.frame = CGRectMake(0, screenHeight - (newSize.height) , screenWidth, newSize.height)
    
    func getScaledSizeOfImage(image: UIImage, toSize: CGSize) -> CGSize       
    {
        let widthRatio = toSize.width/image.size.width
        let heightRatio = toSize.height/image.size.height
        let scale = min(widthRatio, heightRatio)
        let imageWidth = scale*image.size.width
        let imageHeight = scale*image.size.height
        return CGSizeMake(imageWidth, imageHeight)
    }
    

提交回复
热议问题