gpt4 book ai didi

ios - TabBar 项目图像大小

转载 作者:行者123 更新时间:2023-11-28 07:59:23 25 4
gpt4 key购买 nike

在我的应用中,用户用相机或从图库中拍照,然后它会显示在项目栏中。

当然,图像的尺寸不适合标签栏项目。

目前我使用一个函数将图像调整为 23x23 像素。但是在 Retina 显示器中,我看到它很模糊,因为我没有 2x 和 3x 版本..

如果我尝试将大小调整为 69x69,即 3x 版本,那么我将在 96x96 中看到它太大了..

我该如何解决这个问题?

这是我更新标签栏项目图片的地方:

   let newImage = UIImage.resizeImage(image: image, targetSize: CGSize(width: 69, height: 69))
print(UIScreen.main.scale)
print(newImage.scale)
items[items.count - 1].image = newImage.convertToGrayScale().withRenderingMode(.alwaysOriginal)
items[items.count - 1].selectedImage = newImage.withRenderingMode(.alwaysOriginal)

这是调整大小的函数:

static func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size

let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height

// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}

// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage!
}

最佳答案

修复了在调整大小函数中使用 UIScreen.main.scale 因子

// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.main.scale)

关于ios - TabBar 项目图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47073418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com