简体   繁体   English

将ImageView调整为UIViewContentModeScaleAspectFit内容的大小

[英]Resizing ImageView as the UIViewContentModeScaleAspectFit content

Sorry if it might sound as a stupid question. 抱歉,这听起来像是一个愚蠢的问题。 I want to get the rect size of an image after i scaled it to fit. 我想缩放图像以适应图像的矩形大小。 One way i thought of doing it is if i could resize the imageView itself and use ".frame", can i do so? 我想到的一种方法是,如果我可以调整imageView本身的大小并使用“ .frame”,我可以这样做吗? is there any other way to get rid from the blank space around? 还有什么其他方法可以摆脱周围的空白?

Based on the contentMode of the imageView being UIViewContentModeScaleAspectFit , once you set the image property of the imageView, you could use the following to determine the size (in points) of the image on the screen: 基于contentModeUIViewContentModeScaleAspectFit ,一旦设置了UIViewContentModeScaleAspectFitimage属性,就可以使用以下方法确定屏幕上图像的大小(以磅为单位):

-(CGSize)onScreenPointSizeOfImageInImageView:(UIImageView *)imageView{
    CGFloat scale;
    if (imageView.frame.size.width > imageView.frame.size.height) {
        if (imageView.image.size.width > imageView.image.size.height) {
            scale = imageView.image.size.height / imageView.frame.size.height;
        } else {
            scale = imageView.image.size.width / imageView.frame.size.width;
        }
    } else {
        if (imageView.image.size.width > imageView.image.size.height) {
            scale = imageView.image.size.width / imageView.frame.size.width;
        } else {
            scale = imageView.image.size.height / imageView.frame.size.height;
        }
    }
    return CGSizeMake(imageView.image.size.width / scale, imageView.image.size.height / scale);
}

Then set the frame of the image view based on the on-screen size of the image to eliminate the 'blank space' around the image, eg; 然后根据图像的屏幕尺寸设置图像视图的框架,以消除图像周围的“空白”。

myImageView.image = myImage;
CGSize imageSize = [self onScreenPointSizeOfImageInImageView:myImageView];
CGRect imageViewRect = myImageView.frame;
imageViewRect.size = imageSize;
myImageView.frame = imageViewRect;

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

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