Reputation: 2110
In Swift I have an image: (but {I think} the problem is how the 'self' is implemented) (in my viewDidLoad)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel = 4
let bitsPerComponent = 8
let bytesPerRow = bytesPerPixel * self.cols // width
let bitmapInfo = RGBA32.bitmapInfo
let minimapContext = CGContext(data: nil, width: self.cols, height: self.rows, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)
let buffer = minimapContext!.data
self.pixelBuffer = buffer!.bindMemory(to: RGBA32.self, capacity: self.cols * self.rows) // was let
var pixelIndex = 1
Then I loop over an array of like:
self.pixelBuffer[pixelIndex] = .blue
This all works fine without the 'self'.
I want to change some of the pixels, so I added the 'self' and defined it at the top of the ViewController class
Now change the pixels like:
self.pixelBuffer[newPixelIndex] = .lightblue
But I get a variety of errors: Cannot assign value of type 'UnsafeMutablePointer' to type 'RGBA32'
Cannot infer contextual base in reference to member 'lightblue'
Value of type 'RGBA32?' has no subscripts
Don't know if this helps but the pixelBuffer is defined ike:
var pixelBuffer: RGBA32? = nil
and here is RGBA32:
struct RGBA32: Equatable {
private var color: UInt32
init(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8) {
color = (UInt32(red) << 24) | (UInt32(green) << 16) | (UInt32(blue) << 8) | (UInt32(alpha) << 0)
}
static let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Little.rawValue
static func ==(lhs: RGBA32, rhs: RGBA32) -> Bool {
return lhs.color == rhs.color
}
static let black = RGBA32(red: 0, green: 0, blue: 0, alpha: 255)
static let red = RGBA32(red: 255, green: 0, blue: 0, alpha: 255)
static let green = RGBA32(red: 24, green: 183, blue: 3, alpha: 255)
static let darkgreen = RGBA32(red: 70, green: 105, blue: 35, alpha: 255)
static let blue = RGBA32(red: 0, green: 127, blue: 255, alpha: 255)
static let lightblue = RGBA32(red: 33, green: 255, blue: 255, alpha: 255)
static let brown = RGBA32(red: 127, green: 63, blue: 0, alpha: 255)
}
THANKS!!
Upvotes: 0
Views: 150
Reputation: 536027
The main problem is that you have declared self.pixelBuffer
to be an Optional RGBA32. But that is not what pixelBuffer
was before. It was a UnsafeMutablePointer<RGBA32>
. Here is the declaration you need:
var pixelBuffer : UnsafeMutablePointer<RGBA32>!
All your issues will then go away, I think. Your
self.pixelBuffer[newPixelIndex] = .lightblue
then compiles for me.
Upvotes: 1