2015-04-01 1 views
0

Je souhaite pouvoir définir tous les pixels de l'écran sur une couleur spécifique. La méthode que j'utilise est la création de tous les pixels dans un tableau de données brutes et ensuite créer un UIImage que je mets dans un UIImageView. Je recadrée le code jusqu'à ce qui montre encore le problème, je pense que je l'ai fait quelque chose de stupide en créant l'image mais j'ai lu toute la documentation et à mon avis, tout semble bien:Transformer un pixel ARGB brut en UIImage

Struct pour sauver pixeldata

public struct PixelData { 
    var a: UInt8 = 255 
    var r: UInt8 
    var g: UInt8 
    var b: UInt8 
} 

matrice de remplissage gauche moitié rouge, moitié droite noir

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 
    } 

image de retour de bitmap

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) 
} 

Et enfin la fonction mise

func updateUI() { 
     let data = halfRood() 
     if let image = imageFromARGB32Bitmap(data, width: UInt(Constants.Resolution.x), height: UInt(Constants.Resolution.y)) { 
      imageView?.image = image 
     } 
    } 

updateUI() La sortie ressemble à l'image suivante, alors que je m'y attendais quelque chose comme: [moitié rouge | moitié noir]

currentSituation

Répondre

2

CoreGraphics attend des données de pixels sous forme de lignes, pas de colonnes. Il suffit de retourner vos pour-déclarations comme ceci:

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 
} 
+0

C'était la solution, merci beaucoup! – Simon