使用Swift中的CoreGraphics在屏幕上绘制像素

使用Swift中的CoreGraphics在屏幕上绘制像素,swift,core-graphics,Swift,Core Graphics,下面的代码试图将像素设置为脱机位图,并将位图绘制到屏幕上。不幸的是,它崩溃了 import UIKit class GameView: UIView { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(frame: CGRect) { super.init(frame: frame) } fu

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

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)
    }

}
导入UIKit
类GameView:UIView{
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
}
重写初始化(帧:CGRect){
super.init(frame:frame)
}
func createBitmapContext(像素宽度:Int,xelshigh:Int)->CGContextRef{
设字节/像素=4
让bytesPerRow=bytesPerPixel*像素宽度
设bitsPerComponent=8
让字节计数=(字节数*像素)
让colorSpace=CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)
let pixels=unsafemeutablepointer.alloc(字节数)
如果像素==nil{
归零
}
设bitmapInfo=CGImageAlphaInfo.PremultipledFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue
let context=CGBitmapContextCreate(像素、像素宽度、像素高度、bitsPerComponent、bytesPerRow、颜色空间、bitmapInfo)
返回上下文
}
重写func drawRect(rect:CGRect){
宽度=200
让高度=300
设boundingBox=CGRectMake(0,0,CGFloat(宽度),CGFloat(高度))
let context=createBitmapContext(宽度、高度)
let data=CGBitmapContextGetData(上下文)
var currentPixel:[UInt32]=未安全比特广播(数据[UInt32].self)
var n=0
对于var j=0;j
需要进行一些更改。 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)
    }
}
重写func drawRect(rect:CGRect){
宽度=200
让高度=300
设boundingBox=CGRectMake(0,0,CGFloat(宽度),CGFloat(高度))
let context=createBitmapContext(宽度、高度)
let data=CGBitmapContextGetData(上下文)
let pixels=不可靠指针(数据)
var n=0
对于var j=0;j
我假设我不需要调用UIImagePNGRepresentation?另外,我分配了bytesperpoixel=4,后来使用了对UInt32的转换,所以我仍然不明白内存崩溃的原因。您的上下文是在BGRA中默认创建的吗?无需调用UIImagePNGRepresentation。我只是为了测试而添加的。你可以删除这个。CGBitmapContextCreate默认顺序为BGRA。