简体   繁体   English

容器上缩放的iOS应用图像

[英]scaled iOS app images on container

How to make photos that are loaded from the site in containers scaled in height and width in my IOS app? 如何在IOS应用程序中以高度和宽度缩放的容器制作从站点加载的照片? In my application some of the photos are stretched (second one from the start), how to fix it? 在我的应用程序中,有些照片被拉伸了(从开始算起第二张),如何解决?

屏幕截图

One technique using CSS would be to assign the images as the background of an element, then scale accordingly: 使用CSS的一种技术是将图像分配为元素的背景,然后相应地缩放:

.imgContainer{
  background-image:url(myImage.jpg); /* <--- add the image as the background */
  background-size:cover; /* <--- scale the image maintaining proportions */
  background-position:center center; /* <--- center the image */
}

More on background-size from MDN 有关MDN background-size更多信息

The background-size CSS property specifies the size of the background images. background-size CSS属性指定背景图像的大小。 The size of the image can be fully constrained or only partially in order to preserve its intrinsic ratio. 图像的大小可以完全限制或仅部分限制,以保持其固有比率。

cover: This keyword specifies that the background image should be scaled to be as small as possible while ensuring both its dimensions are greater than or equal to the corresponding dimensions of the background positioning area. cover:该关键字指定背景图像应缩放到尽可能小,同时确保其尺寸都大于或等于背景定位区域的相应尺寸。

..and background-position ..和background-position

instead of using width:100% and height:100% on the images, try to use max-width:100% and max-height:100% . 而不是在图像上使用width:100%height:100% ,请尝试使用max-width:100%max-height:100% This way, your images will keep their ratio but may leave blank space. 这样,您的图像将保持其比例,但可能会留有空白。

To avoid blank spaces you could show your images in CSS with background-image:url('url_of_image') and background-size:cover . 为了避免空格,您可以在CSS中使用background-image:url('url_of_image')background-size:cover In this way, the images will fill the div, keeping their ratio but may be of greater size. 这样,图像将填充div,并保持其比例,但尺寸可能更大。

I think you problem is actual size of image, that may not be actual square or rectangle. 我认为您的问题是图片的实际尺寸,可能不是实际的正方形或矩形。 In that case you may want to use this 2 approaches : 在这种情况下,您可能要使用以下两种方法:

    1) yourImageView.contentMode = UIViewContentModeScaleAspectFit;
//That keeps an entire image in frame but cut borders

    2) yourImageView.contentMode = UIViewContentModeScaleAspectFill;
//That fills whole frame with image but cut off sides

For proper sizing your image in frame i recommend use category: 为了在框架中正确调整图像的大小,我建议使用类别:

- (UIImage*)resizedImageWithBounds:(CGSize)bounds;

- (UIImage*)resizedImageWithBounds:(CGSize)bounds{

// Based on code by Trevor Harmon
    CGFloat horizontalRatio = bounds.width/self.size.width;
    CGFloat verticalRatio = bounds.height/self.size.height;
    CGFloat ratio = MIN(horizontalRatio,verticalRatio);
    CGSize newSize = CGSizeMake(self.size.width*ratio, self.size.height*ratio);

    UIGraphicsBeginImageContextWithOptions(newSize, YES, 0);

    if (newSize.width >66){
        newSize.width = 66;
    }

    if (newSize.height > 66){
        newSize.height = 66;
    }
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];


    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

In this code 66 stand for width/height of an image. 在此代码66中,代表图像的宽度/高度。

Hope it helps. 希望能帮助到你。

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

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