osx - How do I draw NSGradient to NSImage? -
i'm trying take nsgradient , save image in rubymotion, can't work. code have far:
gradient = nsgradient.alloc.initwithcolors(colors, atlocations: locations.to_pointer(:double), colorspace: nscolorspace.genericrgbcolorspace ) size = size(width, height) image = nsimage.imagewithsize(size, flipped: false, drawinghandler: lambda |rect| gradient.drawinrect(rect, angle: angle) true end) data = image.tiffrepresentation data.writetofile('output.tif', atomically: false)
it runs without error, file saved blank , there no image data. can point me in right direction?
i don’t know rubymotion, here’s how in objective-c:
nsgradient *grad = [[nsgradient alloc] initwithstartingcolor:[nscolor redcolor] endingcolor:[nscolor bluecolor]]; nsrect rect = cgrectmake(0.0, 0.0, 50.0, 50.0); nsimage *image = [[nsimage alloc] initwithsize:rect.size]; nsbezierpath *path = [nsbezierpath bezierpathwithrect:rect]; [image lockfocus]; [grad drawinbezierpath:path angle:0.0]; nsbitmapimagerep *imgrep = [[nsbitmapimagerep alloc] initwithfocusedviewrect:rect]; nsdata *data = [imgrep representationusingtype:nspngfiletype properties:nil]; [image unlockfocus]; [data writetofile: @"/path/to/file.png" atomically:no];
Comments
Post a Comment