This is a continuation of this thread. My first question was answered and so I thought it would be rude to keep commenting and extending the question.
Using the below code, I am converting an RGBA image into an array of integers. However, when I convert back into an image, the data is weird. My back-conversion process is not the issue, because when I debug the pixel array upon creation, the pixels do not match the original image: They match the distorted one.
I'm wondering what might be the source of these issues.
The code:
init?(fromImage image: UIImage!) {
let imageRef = image!.CGImage
self.width = CGImageGetWidth(imageRef)
self.height = CGImageGetHeight(imageRef)
let colorspace = CGColorSpaceCreateDeviceRGB()
let bytesPerRow = (4 * width);
let bitsPerComponent :UInt = 8
let pixels = UnsafeMutablePointer<UInt8>(malloc(width*height*4))
var context = CGBitmapContextCreate(pixels, width, height, Int(bitsPerComponent), bytesPerRow, colorspace, CGImageAlphaInfo.PremultipliedLast.rawValue);
CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), imageRef)
for row in 0 ..< height {
for col in 0 ..< width {
let offset = 4 * (width * row) + col
print("\(pixels[offset]) ", terminator:"")
print("\(pixels[offset + 1]) ", terminator:"")
print("\(pixels[offset + 2]) ", terminator:"")
print("\(pixels[offset + 3]) ", terminator:"")
print(" | ", terminator:"")
}
print(" ")
}
}
Anything helps! Thanks again.