Monday, 15 February 2010

Convert an image into a 2D array (or equivalent) in Apple Swift -



Convert an image into a 2D array (or equivalent) in Apple Swift -

i'm wondering how can turn uiimage usable , modifiable. java code handle need this:

bufferedimage img= imageio.read(file); raster raster=img.getdata(); int w=raster.getwidth(),h=raster.getheight(); int pixels[][]=new int[w][h]; (int x=0;x<w;x++) { for(int y=0;y<h;y++) { pixels[x][y]=raster.getsample(x,y,0); } }

i need modify alpha values in image visiting each pixel in image.

untested, think either work or should close.

import uikit import coregraphics var uiimage = uiimage(contentsoffile: "/path/to/image.png") var image = uiimage.cgimage allow width = cgimagegetwidth(image) allow height = cgimagegetheight(image) allow colorspace = cgcolorspacecreatedevicergb() allow bytesperrow = (4 * width); allow bitspercomponent :uint = 8 allow pixels = unsafepointer<uint8>(malloc(width*height*4)) var context = cgbitmapcontextcreate(pixels, width, height, bitspercomponent, bytesperrow, colorspace, cgbitmapinfo()); cgcontextdrawimage(context, cgrectmake(0, 0, cgfloat(width), cgfloat(height)), image) x in 0..width { y in 0..height { //here raw pixels allow offset = 4*((int(width) * int(y)) + int(x)) allow alpha = pixels[offset] allow reddish = pixels[offset+1] allow greenish = pixels[offset+2] allow bluish = pixels[offset+3] } }

image apple swift multidimensional-array

No comments:

Post a Comment