swift - Turning raw ARGB pixeldata into UIImage -


i want able set pixels on screen specific color. method using creating pixels in raw data array , create uiimage put in uiimageview. cropped code down still showing problem, think have done stupid creating image have read documentation , imho seems fine:

struct saving pixeldata

public struct pixeldata {     var a: uint8 = 255     var r: uint8     var g: uint8     var b: uint8 } 

filling array left half red, right half black

func halfrood()->[pixeldata] {         var data: [pixeldata] = []          for(var = 0; < 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 image 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) } 

and setting updateui() function

func updateui() {         let data = halfrood()         if let image = imagefromargb32bitmap(data, width: uint(constants.resolution.x), height: uint(constants.resolution.y)) {             imageview?.image = image         }     } 

the output looks following picture, while expected like: [red half | black half]

currentsituation

coregraphics expects pixel data rows, not columns. flip for-statements this:

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 } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -