【问题标题】:Filling UIBezierPath results in pixelated image from image context [duplicate]填充 UIBezierPath 会导致图像上下文中的像素化图像[重复]
【发布时间】:2023-03-28 21:15:01
【问题描述】:

我使用UIBezierPath 实现了一个非常简单的填充圆圈,然后我将其转换为UIImage,这样我就可以将其设置为UITableViewCellimageView。这非常有效,但您可以在 Retina 显示屏上看到边缘像素化。为什么会这样,可以做些什么来确保它看起来很棒?

let colorSize = cell.frame.size.height - 20
let rect = CGRectMake(0, 0, colorSize, colorSize)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
UIBezierPath(roundedRect: rect, cornerRadius: (colorSize)/2).addClip()
CGContextSetFillColorWithColor(context, UIColor.redColor().CGColor);
CGContextFillRect(context, rect)
var colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
cell.imageView?.image = colorImage

【问题讨论】:

    标签: ios swift uiimage cgcontext uibezierpath


    【解决方案1】:

    第 1 步。阅读UIGraphicsBeginImageContext documentation

    这个函数相当于调用UIGraphicsBeginImageContextWithOptions函数,不透明参数设置为false,比例因子为1.0

    第 2 步。点击UIGraphicsBeginImageContextWithOptions documentation 的链接,上面写着:

    scale 应用于位图的比例因子。如果您指定值0.0,则比例因子设置为设备主屏幕的比例因子。

    第 3 步。尝试使用UIGraphicsBeginImageContextWithOptions0.0 的比例:

    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
    

    第 4 步。利润……?

    【讨论】:

    • -1 表示刻薄。
    • +1 包括第 4 步
    猜你喜欢
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2020-07-29
    • 2014-08-09
    • 2017-06-17
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    相关资源
    最近更新 更多