TabBar项目图像大小

时间:2017-11-02 10:48:16

标签: ios swift uiimage uitabbarcontroller

在我的应用程序中,用户使用相机或图书馆拍照,然后它将显示在项目栏中。

当然,图片的标签栏项目大小不合适。

目前我使用一个函数将图像大小调整为23x23像素。但在视网膜显示器中,我看到它模糊,因为我没有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!
    }

1 个答案:

答案 0 :(得分:0)

使用调整大小函数

中的UIScreen.main.scale因子进行了修复
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.main.scale)