菜鸟教程小白 发表于 2022-12-11 16:42:44

ios - 为什么我的 CGImage 在 setNeedsDisplay 后无法正确显示


                                            <p><p>我正在我的应用程序中绘制一个 colorWheel。我通过在单独的函数中生成 CGImage 并在 drawRect 中使用 CGContextDrawImage 将其放在屏幕上来做到这一点。</p>

<p>在最初的演示中,它看起来不错,但在我调用 setNeedsDisplay(或 setNeedsDisplayInRect)之后,图像变黑/失真。我一定在做一些愚蠢的事情,但看不到什么。</p>

<p> <img src="https://i.imgur.com/TiZDuaw.png" alt="Cool Wheel"/>
<img src="https://i.imgur.com/jrQDtqn.png" alt="Cool Wheel"/> </p>

<p>DrawRect 代码如下:</p>

<pre><code>override func drawRect(rect: CGRect) {

    if let context = UIGraphicsGetCurrentContext() {

      let wheelFrame = CGRectMake(0,0,circleRadius*2,circleRadius*2)
      CGContextSaveGState(context)

            //create clipping mask for circular wheel
      CGContextAddEllipseInRect(context, wheelFrame)
      CGContextClip(context)

            //draw the wheel
      if colorWheelImage != nil { CGContextDrawImage(context, CGRectMake(0,0,circleRadius*2,circleRadius*2), colorWheelImage) }
      CGContextRestoreGState(context)

            //draw a selector element
      self.drawSliderElement(context)
    }
}
</code></pre>

<p>CG图像生成函数:</p>

<pre><code>func generateColorWheelImage() {

    let width       = Int(circleRadius*2)
    let height      = Int(circleRadius*2)

    var pixels = ()

    for yIndex in 0..&lt;width {
      for xIndex in 0..&lt;height {
            pixels.append(colorAtPoint(CGPoint(x: xIndex, y: yIndex)))
      }
    }

    let rgbColorSpace   = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo      = CGBitmapInfo.ByteOrderDefault

    let bitsPerComponent: Int   = 8
    let bitsPerPixel: Int       = 24

    let renderingIntent = CGColorRenderingIntent.RenderingIntentDefault

    assert(pixels.count == Int(width * height))

    var data = pixels

    let providerRef = CGDataProviderCreateWithData(nil, data, data.count * sizeof(PixelData), nil)

    colorWheelImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, width * Int(sizeof(PixelData)), rgbColorSpace, bitmapInfo, providerRef, nil, true,renderingIntent)

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当我开始在 ipad 而不是模拟器上运行代码并收到 BAD_ACCESS_ERROR,而不是看到扭曲的图像时,我终于找到了这个问题的答案(这里:<a href="https://stackoverflow.com/a/10798750/5233176" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/10798750/5233176</a>)。</p>

<p>正如答案所解释的:' CMSampleBuffer 直接用于创建 CGImageRef,因此一旦释放缓冲区, CGImageRef 将变得无效。'</p>

<p>因此问题出在这一行:</p>

<pre><code>    let providerRef = CGDataProviderCreateWithData(nil, data, data.count * sizeof(PixelData), nil)
</code></pre>

<p>并且可以通过使用缓冲区的副本来解决问题,如下所示:</p>

<pre><code>    let providerData    = NSData(bytes: data, length: data.count * sizeof(PixelData))
    let provider      = CGDataProviderCreateWithCFData(providerData)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么我的 CGImage 在 setNeedsDisplay 后无法正确显示,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37773844/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37773844/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么我的 CGImage 在 setNeedsDisplay 后无法正确显示