2016-01-03 38 views
1
let colorSpace = CGColorSpaceCreateDeviceRGB() 
    let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.ByteOrder32Big.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue) 


    let bitmap = CGBitmapContextCreate(nil, 192, 192, 8, 0, colorSpace,bitmapInfo.rawValue) 

    let bitmapData = UnsafeMutablePointer<UInt32>(CGBitmapContextGetData(bitmap)) 
    var j = 0 
    for var i = 0; i < 192; i++ { 
     for j = 0; j < 192; j = j + 16 { 
      for var k = 0; k < 16; k++ { 

       let offset = matrix.rows * i + j + k 
       if(k < 4){ 
        bitmapData[offset] = 0xff0000ff //red 
       } 
       else{ 
        bitmapData[offset] = 0xffff00ff //yellow 
       } 
      } 
     } 
    } 

    let imageRef = CGBitmapContextCreateImage(bitmap) 
    let image = UIImage(CGImage: imageRef!) 

以上是我从原始像素创建图像的代码。我设置的颜色是0xff0000ff和0xffff00ff。但是,当图像显示在UIimageView中时,它会显示一些随机颜色或无法显示颜色。从原始像素创建图像,但颜色无法通过RGB十六进制值显示

例如,currenctly颜色是0xffff00ff和0xff0000ff,但显示的内容我的应用程序是这样的:

enter image description here

有谁知道如何设置颜色为位图?

+0

你可能只需要尝试ARGB而不是RGBA。在红色,绿色和蓝色值之前放置你的alpha。 –

回答

2

我在输出中看到红色和洋红色。你说你使用颜色值0xffff00ff0xff0000ff。因此我推断你的位图中的字节顺序是ABGR。

如果您将bitmapInfo更改为CGBitmapInfo.ByteOrder32Little.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue,您将获得您期望的字节顺序。

+1

你能解释一下什么之间的区别“CGBitmapInfo.ByteOrder32Little.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue”和“CGBitmapInfo.ByteOrder32Big.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue” – shixiaoz

+0

一放字节RGBA odrer,另一个使他们在ABGR订单。 –

+0

但我试过,如果我删除CGBitmapInfo.ByteOrder32Big.rawValue这个条件,结果是无法显示,为什么? – shixiaoz

相关问题