将原始 RGB 像素数据转换为 UIImage
Turning raw ARGB pixeldata into UIImage
我希望能够将屏幕上的所有像素设置为特定颜色。我使用的方法是在原始数据数组中创建所有像素,然后创建一个 UIImage,我将其放入 UIImageView。我将代码裁剪到这个仍然显示问题,我认为我通过创建图像做了一些愚蠢的事情但是我已经阅读了所有文档并且恕我直言一切似乎都很好:
保存像素数据的结构体
public struct PixelData {
var a: UInt8 = 255
var r: UInt8
var g: UInt8
var b: UInt8
}
填充数组左半边红右半边黑
func halfRood()->[PixelData] {
var data: [PixelData] = []
for(var i = 0; i < Int(Constants.Resolution.x); i++) {
for(var j = 0; j < Int(Constants.Resolution.y); j++) {
if(j < Int(Constants.Resolution.y/2)) {
data.append(PixelData(a: 255, r: 255, g: 0, b: 0))
} else {
data.append(PixelData(a: 255, r: 0, g: 0, b: 0))
}
}
}
return data
}
Return 来自位图的图像
func imageFromARGB32Bitmap(pixels: [PixelData], width: UInt, height: UInt) -> UIImage? {
let bitsPerComponent: UInt = 8
let bitsPerPixel: UInt = 32
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo:CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue)
var data = pixels
let providerRef = CGDataProviderCreateWithCFData(NSData(bytes: &data, length: data.count * sizeof(PixelData)))
let providerRefthing: CGDataProvider = providerRef
let cgImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, width * UInt(sizeof(PixelData)), rgbColorSpace, bitmapInfo, providerRef, nil, true, kCGRenderingIntentDefault)
let cgiimagething: CGImage = cgImage
return UIImage(CGImage: cgImage)
}
最后设置 updateUI() 函数
func updateUI() {
let data = halfRood()
if let image = imageFromARGB32Bitmap(data, width: UInt(Constants.Resolution.x), height: UInt(Constants.Resolution.y)) {
imageView?.image = image
}
}
输出看起来像下图,而我期望是这样的:
[红一半|黑色一半]
CoreGraphics 需要像素数据作为行,而不是列。像这样翻转你的 for 语句:
func halfRood()->[PixelData] {
var data: [PixelData] = []
for(var y = 0; y < Int(Constants.Resolution.y); y++) {
for(var x = 0; x < Int(Constants.Resolution.x); x++) {
if(y < Int(Constants.Resolution.y/2)) {
data.append(PixelData(a: 255, r: 255, g: 0, b: 0))
} else {
data.append(PixelData(a: 255, r: 0, g: 0, b: 0))
}
}
}
return data
}
我希望能够将屏幕上的所有像素设置为特定颜色。我使用的方法是在原始数据数组中创建所有像素,然后创建一个 UIImage,我将其放入 UIImageView。我将代码裁剪到这个仍然显示问题,我认为我通过创建图像做了一些愚蠢的事情但是我已经阅读了所有文档并且恕我直言一切似乎都很好:
保存像素数据的结构体
public struct PixelData {
var a: UInt8 = 255
var r: UInt8
var g: UInt8
var b: UInt8
}
填充数组左半边红右半边黑
func halfRood()->[PixelData] {
var data: [PixelData] = []
for(var i = 0; i < Int(Constants.Resolution.x); i++) {
for(var j = 0; j < Int(Constants.Resolution.y); j++) {
if(j < Int(Constants.Resolution.y/2)) {
data.append(PixelData(a: 255, r: 255, g: 0, b: 0))
} else {
data.append(PixelData(a: 255, r: 0, g: 0, b: 0))
}
}
}
return data
}
Return 来自位图的图像
func imageFromARGB32Bitmap(pixels: [PixelData], width: UInt, height: UInt) -> UIImage? {
let bitsPerComponent: UInt = 8
let bitsPerPixel: UInt = 32
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo:CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue)
var data = pixels
let providerRef = CGDataProviderCreateWithCFData(NSData(bytes: &data, length: data.count * sizeof(PixelData)))
let providerRefthing: CGDataProvider = providerRef
let cgImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, width * UInt(sizeof(PixelData)), rgbColorSpace, bitmapInfo, providerRef, nil, true, kCGRenderingIntentDefault)
let cgiimagething: CGImage = cgImage
return UIImage(CGImage: cgImage)
}
最后设置 updateUI() 函数
func updateUI() {
let data = halfRood()
if let image = imageFromARGB32Bitmap(data, width: UInt(Constants.Resolution.x), height: UInt(Constants.Resolution.y)) {
imageView?.image = image
}
}
输出看起来像下图,而我期望是这样的: [红一半|黑色一半]
CoreGraphics 需要像素数据作为行,而不是列。像这样翻转你的 for 语句:
func halfRood()->[PixelData] {
var data: [PixelData] = []
for(var y = 0; y < Int(Constants.Resolution.y); y++) {
for(var x = 0; x < Int(Constants.Resolution.x); x++) {
if(y < Int(Constants.Resolution.y/2)) {
data.append(PixelData(a: 255, r: 255, g: 0, b: 0))
} else {
data.append(PixelData(a: 255, r: 0, g: 0, b: 0))
}
}
}
return data
}