我有一个tableView单元格,它有一个图像视图。 对于这个视图,我创建了UIImage扩展,我用bezierPath 2矩形绘制,然后,从这个绘图中我创建了一个图像。
class func drawTwoRectangles(_ frameOne: CGRect, frameTwo: CGRect, frameColor: UIColor) -> UIImage {
UIGraphicsBeginImageContext(CGSize(width: frameOne.size.width, height: frameOne.size.height))
let context = UIGraphicsGetCurrentContext()
UIColor(red: 0/255.0, green: 153/255.0, blue: 216/255.0, alpha: 1.0).setFill()
let bpath:UIBezierPath = UIBezierPath(roundedRect:CGRect(origin: CGPoint(x:1, y:1), size: CGSize(width: frameOne.size.width - 2, height: frameOne.size.height - 2)), byRoundingCorners: [.allCorners], cornerRadii: CGSize(width: 5, height: 5))
bpath.lineCapStyle = .round
bpath.lineJoinStyle = .round
bpath.close()
bpath.fill()
UIColor.white.setFill()
UIColor.white.setStroke()
let bpathTwo:UIBezierPath = UIBezierPath(rect: frameTwo)
bpathTwo.close()
bpathTwo.fill()
bpathTwo.stroke()
frameColor.setStroke()
let bpathThree:UIBezierPath = UIBezierPath(roundedRect: CGRect(origin: CGPoint(x:1, y:1), size: CGSize(width: frameOne.size.width - 2, height: frameOne.size.height - 2)), cornerRadius: 5)
bpathThree.lineWidth = 2
bpathThree.close()
bpathThree.stroke()
bpath.append(bpathTwo)
bpath.append(bpathThree)
context?.addPath(bpath.cgPath)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
请问您的建议,如何处理? 提前谢谢!
答案 0 :(得分:1)
尝试替换它:
UIGraphicsBeginImageContext(CGSize(width: frameOne.size.width, height: frameOne.size.height))
用这个:
UIGraphicsBeginImageContextWithOptions(frameOne.size, false, UIScreen.main.scale)
顺便说一下,根据documentation,您可以将0.0
作为第三个参数传递:
要应用于位图的比例因子。如果指定值0.0,则比例因子将设置为设备主屏幕的比例因子。