【发布时间】:2016-07-25 16:57:13
【问题描述】:
这是this thread 的延续。我的第一个问题得到了回答,所以我认为继续评论和扩展问题是不礼貌的。
使用下面的代码,我将 RGBA 图像转换为整数数组。但是,当我转换回图像时,数据很奇怪。 我的反向转换过程不是问题,因为当我在创建时调试像素阵列时,像素与原始图像不匹配:它们与扭曲的图像匹配。
我想知道这些问题的根源是什么。
代码:
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(" ")
}
}
任何帮助!再次感谢。
【问题讨论】:
标签: ios arrays swift image int