【问题标题】:Low Quality TabBarItem Image低质量的 TabBarItem 图像
【发布时间】:2021-06-16 04:00:17
【问题描述】:

我有一张tabbarcontroller,其中一张tabbaritem 图像显示模糊且质量低下。由于此图片是个人资料图片,并且可能与用户输入不同,I have helper functions to assist in squaring, resizing and rounding the image

我找不到任何解决此问题的 SO 答案,任何想法都将不胜感激。

extension UIImage {

    var roundMyImage: UIImage {
        let rect = CGRect(origin:CGPoint(x: 0, y: 0), size: self.size)
        UIGraphicsBeginImageContextWithOptions(self.size, false, 0)
        UIBezierPath(
            roundedRect: rect,
            cornerRadius: self.size.height
            ).addClip()
        self.draw(in: rect)
        return UIGraphicsGetImageFromCurrentImageContext()!
    }
    
    func resizeMyImage(newWidth: CGFloat) -> UIImage {
        let scale = newWidth / self.size.width
        let newHeight = self.size.height * scale
        UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))

        self.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
    }
    
    func squareMyImage() -> UIImage {
        UIGraphicsBeginImageContext(CGSize(width: self.size.width, height: self.size.width))

        self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.width))

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
    }
}

let newImage: UIImage = someImage.squareMyImage().resizeMyImage(newWidth: 24).roundMyImage.withRenderingMode(.alwaysOriginal)
            firstTabNav.tabBarItem.image = newImage
            firstTabNav.tabBarItem.selectedImage = newImage

【问题讨论】:

    标签: ios swift uiimage uitabbarcontroller uitabbaritem


    【解决方案1】:

    没有选项的 UIGraphicsBeginImageContext 给你一个比例设置为 1.0 的上下文 - 对于视网膜屏幕,这不是你想要的。如果您使用 UIGraphicsBeginImageContextWithOptions 您可以传递 0.0 进行缩放,并且根据文档:“如果您指定 0.0 的值,则比例因子设置为设备主屏幕的比例因子” - 您似乎有不一致的比例值squareMyImage、roundMyImage 和 resizeMyImage

    【讨论】:

    • 就是这样!意识到需要在我使用的所有辅助函数中应用 0 比例!谢谢!
    猜你喜欢
    • 2020-02-14
    • 2014-10-14
    • 2019-08-23
    • 2014-12-24
    • 2017-08-03
    • 1970-01-01
    • 2021-10-27
    • 2018-08-09
    • 1970-01-01
    相关资源
    最近更新 更多