重新缩放图像使宽度是屏幕大小的一半 [英] Rescale image so width is half the size of screen

查看:25
本文介绍了重新缩放图像使宽度是屏幕大小的一半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 swift 还很陌生,我目前正在尝试在自定义 UITableCell 中显示多个图像视图.

I'm fairly new to swift and I'm currently trying to display multiple image views within a custom UITableCell.

理想情况下,我希望对每个图像进行缩放,使其宽度填充屏幕尺寸的一半(同时保留原始纵横比).

Ideally, I would like to have each image be scaled so that its width fills half of the screen size (while preserving the original aspect ratio).

这个想法是每个 UITableCell 将有 3 个图像的拼贴画和 UILabel 中的一些附加信息——最高的图像将位于自己的列中,较小的两个堆叠在最高的旁边.以下是帮助说明我想要实现的目标的屏幕截图:

The idea is that each UITableCell will have a collage of 3 images and some additional information in a UILabel -- the tallest image would sit in its own column with the smaller two stacked right next to the tallest. Below is a screenshot to help illustrate what I'm trying to achieve:

但是,我在这方面遇到了一些麻烦.目前,一旦我使用 Alamofire .GET 请求检索到图像,我就会调用执行以下操作的函数:

However, I'm having some trouble with this. Currently, once I've retrieved an image with an Alamofire .GET request, I call a function that does the below:

let aspectRatio = image.size.width / image.size.height
let newHeight = desiredWidth / aspectRatio

UIGraphicsBeginImageContext(CGSizeMake(desiredWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, desiredWidth, newHeight))

let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return scaledImage

返回图像后,在将其添加到第一个图像视图之前,我按如下方式更新图像视图的框架:

Once the image is returned, before adding it to the first image view, I update the frame of the image view as follows:

      cell.img_1.frame =  CGRect(x: 0, y: 0, width: new_image.size.width, height: new_image.size.height)

      cell.img_1.image = new_image

但是,当我运行代码时,图像的宽度不是屏幕的一半——而是——它们看起来与我将它们放入图像视图而不缩放时完全相同(ImageView 设置为方面适合模式).

However, when I run the code, the width of the images is not half of the screen-- rather- they look exactly the same as they would if I put them into the image view without scaling (the ImageView is set to Aspect Fit Mode).

当我打印出新缩放图像的宽度和高度时,值与我想要的一致.

When I print out the width and height of the newly scaled images the values ARE consistent with what I want.

我确实注意到缩放前和缩放后的纵横比略微偏离了 ~.01.-- 所以鉴于图像视图设置为 Aspect Fit,这能解释我所看到的吗?

I do notice that the pre-scale and post-scale aspect ratios are slightly off by ~.01. -- so given that the image view is set to Aspect Fit, could this explain what I am seeing?

我也尝试过将模式设置为纵横比填充",但看到某些图像的宽度没有正确缩小并占据屏幕一半以上的情况.

I've also tried this with the mode set as Aspect Fill, but saw cases where the width of some images do not get scaled down properly, and take up more than half of the screen.

对于这个冗长的问题深表歉意,非常感谢您对此的帮助,如果需要,我会提供更多信息.谢谢!

Apologies for the long-winded question, would greatly appreciate some help with this and I will provide more information if needed. Thank you!

推荐答案

ImageView 设置为 Aspect Fit Mode).

the ImageView is set to Aspect Fit Mode).

但这是您的问题 - 或其中之一.如果您不希望图像视图调整图像大小,请不要使用任何aspect"内容模式,也不要使用任何fit"或fill"内容模式.例如,使用中心.

But there's your problem - or one of them. If you don't want an image view to resize the image, don't use any "aspect" content mode, nor any "fit" or "fill" content mode. Use, for example, Center.

这篇关于重新缩放图像使宽度是屏幕大小的一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆