【问题标题】:Drawing pixels on the screen using CoreGraphics in Swift在 Swift 中使用 CoreGraphics 在屏幕上绘制像素
【发布时间】:2016-01-25 07:45:05
【问题描述】:

下面的代码试图将像素设置为离线位图并将位图绘制到屏幕上。不幸的是,它崩溃了。

import UIKit

class GameView: UIView {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }


    func createBitmapContext(pixelsWide: Int, _ pixelsHigh: Int) -> CGContextRef? {
        let bytesPerPixel = 4
        let bytesPerRow = bytesPerPixel * pixelsWide
        let bitsPerComponent = 8

        let byteCount = (bytesPerRow * pixelsHigh)

        let colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)

        let pixels = UnsafeMutablePointer<CUnsignedChar>.alloc(byteCount)
        if pixels == nil {
            return nil
        }
        let bitmapInfo = CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue

        let context = CGBitmapContextCreate(pixels, pixelsWide, pixelsHigh, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)

        return context
    }

    override func drawRect(rect: CGRect) {
        let width  = 200
        let height = 300
        let boundingBox = CGRectMake(0, 0, CGFloat(width), CGFloat(height))
        let context = createBitmapContext(width, height)

        let data = CGBitmapContextGetData(context)
        var currentPixel: [UInt32] = unsafeBitCast(data, [UInt32].self)

        var n = 0
        for var j = 0; j < height; j++ {
            for var i = 0; i < width; i++ {
                currentPixel[n++] = 0
            }
        }

        let image = CGBitmapContextCreateImage(context!)
        CGContextDrawImage(context!, boundingBox, image)
    }

}

【问题讨论】:

    标签: swift core-graphics


    【解决方案1】:

    需要进行一些更改。 1. 内存溢出导致崩溃。 2. 您正在从新创建的上下文创建图像并写入相同的上下文而不是当前的绘图上下文。

    使用这个修改过的drawRect函数:

    override func drawRect(rect: CGRect) {
        let width  = 200
        let height = 300
        let boundingBox = CGRectMake(0, 0, CGFloat(width), CGFloat(height))
        let context = createBitmapContext(width, height)
    
        let data = CGBitmapContextGetData(context)
    
        let pixels = UnsafeMutablePointer<CUnsignedChar>(data)
        var n = 0
        for var j = 0; j < height; j++ {
            for var i = 0; i < width; i++ {
                pixels[n++] = 0 //B
                pixels[n++] = 255 //G
                pixels[n++] = 0 //R
                pixels[n++] = 255 //A
            }
        }
        let image = CGBitmapContextCreateImage(context!)
        if let currentContext: CGContext! = UIGraphicsGetCurrentContext() {
            UIImagePNGRepresentation(UIImage(CGImage:image!))?.writeToFile("/Users/admin/Desktop/aaaaa.png", atomically: true)
            CGContextDrawImage(currentContext, boundingBox, image)
        }
    }
    

    【讨论】:

    • 我假设我不需要调用 UIImagePNGRepresentation?另外,我分配了 bytesPerPixel=4,后来使用了 UInt32 的强制转换,所以我仍然不明白为什么内存崩溃。您的上下文是在 BGRA 中默认创建的吗?
    • 无需调用 UIImagePNGRepresentation。我添加只是为了测试目的。您可以删除它。 CGBitmapContextCreate 默认顺序是BGRA。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多