UIImageView aspect fit and center

前端 未结 10 935
执笔经年
执笔经年 2020-12-07 09:48

I have an image view, declared programmatically, and I am setting its image, also programmatically.

However, I find myself unable to set the image to both fit the as

相关标签:
10条回答
  • 2020-12-07 10:15

    This subclass uses center if the image is not larger than the view, otherwise it scales down. I found this useful for a UIImageView that changes the size.

    The image it displays is smaller than the view for large sizes, but larger than the view for small sizes. I want it only to scale down, but not up.

    class CenterScaleToFitImageView: UIImageView {
        override var bounds: CGRect {
            didSet {
                adjustContentMode()
            }
        }
    
        override var image: UIImage? {
            didSet {
                adjustContentMode()
            }
        }
    
        func adjustContentMode() {
            guard let image = image else {
                return
            }
            if image.size.width > bounds.size.width ||
                image.size.height > bounds.size.height {
                contentMode = .ScaleAspectFit
            } else {
                contentMode = .Center
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 10:17

    In swift language we can set content mode of UIImage view like following as:

    let newImgThumb = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
    newImgThumb.contentMode = .scaleAspectFit
    
    0 讨论(0)
  • 2020-12-07 10:17

    Swift

    yourImageView.contentMode = .center
    

    You can use the following options to position your image:

    • scaleToFill
    • scaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
    • redraw // redraw on bounds change (calls -setNeedsDisplay)
    • center // contents remain same size. positioned adjusted.
    • top
    • bottom
    • left
    • right
    • topLeft
    • topRight
    • bottomLeft
    • bottomRight
    0 讨论(0)
  • 2020-12-07 10:17

    I solved this problem like this.

    1. setImage to UIImageView (with UIViewContentModeScaleAspectFit)
    2. get imageSize (CGSize imageSize = imageView.image.size)
    3. UIImageView resize. [imageView sizeThatFits:imageSize]
    4. move position where you want.

    I wanted to put UIView on the top center of UICollectionViewCell. so, I used this function.

    - (void)setImageToCenter:(UIImageView *)imageView
    {
        CGSize imageSize = imageView.image.size;
        [imageView sizeThatFits:imageSize];
        CGPoint imageViewCenter = imageView.center;
         imageViewCenter.x = CGRectGetMidX(self.contentView.frame);
        [imageView setCenter:imageViewCenter];
    }
    

    It works for me.

    0 讨论(0)
  • 2020-12-07 10:19

    Just pasting the solution:

    Just like @manohar said

    imageView.contentMode = UIViewContentModeCenter;
    if (imageView.bounds.size.width > ((UIImage*)imagesArray[i]).size.width && imageView.bounds.size.height > ((UIImage*)imagesArray[i]).size.height) {
           imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    

    solved my problem

    0 讨论(0)
  • You can achieve this by setting content mode of image view to UIViewContentModeScaleAspectFill.

    Then use following method method to get the resized uiimage object.

    - (UIImage*)setProfileImage:(UIImage *)imageToResize onImageView:(UIImageView *)imageView
    {
        CGFloat width = imageToResize.size.width;
        CGFloat height = imageToResize.size.height;
        float scaleFactor;
        if(width > height)
        {
            scaleFactor = imageView.frame.size.height / height;
        }
        else
        {
            scaleFactor = imageView.frame.size.width / width;
        }
    
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(width * scaleFactor, height * scaleFactor), NO, 0.0);
        [imageToResize drawInRect:CGRectMake(0, 0, width * scaleFactor, height * scaleFactor)];
        UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return resizedImage;
    }
    

    Edited Here (Swift Version)

    func setProfileImage(imageToResize: UIImage, onImageView: UIImageView) -> UIImage
    {
        let width = imageToResize.size.width
        let height = imageToResize.size.height
    
        var scaleFactor: CGFloat
    
        if(width > height)
        {
            scaleFactor = onImageView.frame.size.height / height;
        }
        else
        {
            scaleFactor = onImageView.frame.size.width / width;
        }
    
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(width * scaleFactor, height * scaleFactor), false, 0.0)
        imageToResize.drawInRect(CGRectMake(0, 0, width * scaleFactor, height * scaleFactor))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        return resizedImage;
    }
    
    0 讨论(0)
提交回复
热议问题