Ios 从swift中的RGBA数据创建UIImage
我有一个像素阵列,需要从中创建UIImage。像素阵列对RGBA格式的每个像素使用4个字节。i、 e.00000000-为黑色透明,000000ff-为黑色不透明 UIImage将使用swift和xcode 6.3或更高版本创建 需要在像素阵列中循环,每四个字节获取一个像素,并从像素中创建图像Ios 从swift中的RGBA数据创建UIImage,ios,swift,rgba,Ios,Swift,Rgba,我有一个像素阵列,需要从中创建UIImage。像素阵列对RGBA格式的每个像素使用4个字节。i、 e.00000000-为黑色透明,000000ff-为黑色不透明 UIImage将使用swift和xcode 6.3或更高版本创建 需要在像素阵列中循环,每四个字节获取一个像素,并从像素中创建图像 可以看到客观的c参考,但不清楚swift的代码示例。以下是我用于从RGBA字节数组创建CGImage的示例: struct m_RGBColor // My own data-type to hold t
可以看到客观的c参考,但不清楚swift的代码示例。以下是我用于从RGBA字节数组创建CGImage的示例:
struct m_RGBColor // My own data-type to hold the picture information
{
var Alpha: UInt8 = 255
var Red: UInt8 = 0
var Green: UInt8 = 0
var Blue: UInt8 = 0
}
var pixelArray: [m_RGBColor] = [m_RGBColor]()
var pixelColor: m_RGBColor = m_RGBColor()
for y in 0 ..< height // Height of your Pixture
{
for x in 0 ..< width // Width of your Picture
{
onePixel.Alpha = 0 // Fill one Pixel with your Picture Data
onePixel.Red = 0 // Fill one Pixel with your Picture Data
onePixel.Green = 0 // Fill one Pixel with your Picture Data
onePixel.Blue = 0 // Fill one Pixel with your Picture Data
pixelArray.append(onePixel)
}
}
let bitmapCount: Int = pixelArray.count
let elmentLength: Int = sizeof(m_RGBColor)
let render: CGColorRenderingIntent = CGColorRenderingIntent.RenderingIntentDefault
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo: CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
let providerRef: CGDataProvider? = CGDataProviderCreateWithCFData(NSData(bytes: &pixelArray, length: bitmapCount * elmentLength))
let cgimage: CGImage? = CGImageCreate(width, height, 8, 32, width * elmentLength, rgbColorSpace, bitmapInfo, providerRef, nil, true, render)
if cgimage != nil
{
// You have success, the Image is valid and usable
}
struct m_RGBColor//我自己的数据类型来保存图片信息
{
变量α:UInt8=255
变量红色:UInt8=0
绿色变量:UInt8=0
变量蓝色:UInt8=0
}
var像素阵列:[m_RGBColor]=[m_RGBColor]()
var pixelColor:m_RGBColor=m_RGBColor()
对于0中的y..
目标c参考在哪里?我可以用swift为您翻译,创建一个带有颜色的图像意味着什么?看看这个问题。