a1049145827/BSText

UIGraphicsBeginImageContext iOS 17 deprecated.

S532128 opened this issue · 2 comments

UIGraphicsBeginImageContextWithOptions(size, _: tmpopaque, _: scale)
let context = UIGraphicsGetCurrentContext()
if tmpopaque && context != nil {
context?.saveGState()
do {
if tmpbackgroundColor != nil || tmpbackgroundColor!.alpha < 1 {
context?.setFillColor(UIColor.white.cgColor)
context?.addRect(CGRect(x: 0, y: 0, width: size.width * scale, height: size.height * scale))
context?.fillPath()
}
if tmpbackgroundColor != nil {
context?.setFillColor(tmpbackgroundColor!)
context?.addRect(CGRect(x: 0, y: 0, width: size.width * scale, height: size.height * scale))
context?.fillPath()
}
}
context?.restoreGState()
}
task?.display!(context, size, isCancelled)
if isCancelled() {
UIGraphicsEndImageContext()
DispatchQueue.main.async(execute: {
if ((task?.didDisplay) != nil) {
task?.didDisplay!(self, false)
}
})
return
}
let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

报错修改为:

let format = UIGraphicsImageRendererFormat()
format.opaque = self.isOpaque
format.scale = contentsScale
let renderer = UIGraphicsImageRenderer(size: bounds.size, format: format)
let image: UIImage? = renderer.image(actions: { rendererContext in
let context = rendererContext.cgContext
if self.isOpaque {
var size: CGSize = bounds.size
size.width *= contentsScale
size.height *= contentsScale
context.saveGState()
do {
if self.backgroundColor == nil || self.backgroundColor!.alpha < 1 {
context.setFillColor(UIColor.white.cgColor)
context.addRect(CGRect(x: 0, y: 0, width: size.width, height: size.height))
context.fillPath()
}
if self.backgroundColor != nil {
context.setFillColor(self.backgroundColor!)
context.addRect(CGRect(x: 0, y: 0, width: size.width, height: size.height))
context.fillPath()
}
}
context.restoreGState()
}
task?.display!(context, bounds.size, {
return false
})
})
self.contents = image?.cgImage
if task?.didDisplay != nil {
task?.didDisplay!(self, true)
}

点赞

TextAsyncLayer 中修改