ios - CGContext 初始化后不绘制

标签 ios swift core-graphics cgcontext

我正在尝试在 UIView draw(_:) 中创建自己的 CGContext。这里:

    override func draw(_ rect: CGRect) {
        //let ctx = UIGraphicsGetCurrentContext()
        let width = Int(rect.size.width)
        let height = Int(rect.size.height)
        let bytesPerPixel = 4
        let bytesPerRow = bytesPerPixel * width
        let data = UnsafeMutableRawPointer.allocate(byteCount: bytesPerRow * height/*727080*/, alignment: 8)
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        guard let ctx = CGContext(data: data, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue) else {
            return
        }
        ctx.setStrokeColor(UIColor.orange.cgColor)
        ctx.setLineWidth(3.0)
        ctx.stroke(rect)
    }

并且 View 没有在显示上勾勒出来,而使用当前上下文可以自然地工作(显示边界边框笔划):

    override func draw(_ rect: CGRect) {
        guard let ctx = UIGraphicsGetCurrentContext() else {
            return
        }
        ctx.setStrokeColor(UIColor.orange.cgColor)
        ctx.setLineWidth(3.0)
        ctx.stroke(rect)
    }

我已经按照 CGContext init 方法的 swift 头文件中的描述进行了操作。这是什么原因与获取当前上下文的功能不同,因为我似乎使用了当前 View 中的位图信息集。

最佳答案

这是一个创建屏幕外内容的简单演示,用于准备某些东西(可能很重,所以不会阻塞 UI)以呈现在 View 中

demo

class ViewWithOffscreenDrawing: UIView {
    private var offscreenImage: CGImage? = nil

    override func draw(_ rect: CGRect) {
        if offscreenImage == nil { // image not ready
            DispatchQueue.global(qos: .background).async { // draw offscreen
                let image = renderSomething(of: rect.size)
                DispatchQueue.main.async { [weak self] in
                    self?.offscreenImage = image
                    self?.setNeedsDisplay()
                }
            }
            return
        }

        // image has prepared - just draw on screen
        UIGraphicsGetCurrentContext()?.draw(offscreenImage!, in: rect)
    }
}

func renderSomething(of size: CGSize) -> CGImage? {
    let bitsPerComponent = 8
    let bytesPerRow = Int(size.width) * 4
    let colorSpace = CGColorSpace(name: CGColorSpace.genericRGBLinear)!

    let context = CGContext(data: nil, width: Int(size.width), height: Int(size.height),
        bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow,
        space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)

    // >>> draw anything heavy in background context

    context?.setFillColor(gray: 1, alpha: 1)
    context?.fill(CGRect(origin: .zero, size: size))
    context?.setFillColor(UIColor.red.cgColor)
    context?.fillEllipse(in: CGRect(origin: .zero, size: size))

    // <<<

    return context?.makeImage() // generated result
}

关于ios - CGContext 初始化后不绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61179237/

相关文章:

iphone - 核心图形-使用中点和角度的线

ios - 如何根据国家/地区将印度时间转换为其他时间?

swift - 如何重置所有全局变量?

iphone - 没有可见的@interface 声明选择器错误

swift - Firebase 身份验证被调用两次

ios - 无法在 UISearchController 上成为 FirstResponder 工作

ios - 更改图像颜色时更改 CGContext 的线宽?

objective-c - 在 iOS 中绘制 1 像素宽的路径

ios - 如何修复 'sizeWithFont:constrainedToSize:lineBreakMode:' 已弃用 : warning

ios - 本地化具有附加后缀的应用程序显示名称