2014-06-10 4 views
0

NSGradient를 가져 와서 RubyMotion에서 이미지로 저장하려하지만 작동하지 않습니다. 이것은 내가 지금까지 가지고있는 코드 :NSImage에 NSGradient를 그려 넣으려면 어떻게해야합니까?

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 do |rect| 
    gradient.drawInRect(rect, angle: angle) 
    true 
end) 

data = image.TIFFRepresentation 
data.writeToFile('output.tif', atomically: false) 

그것은 오류없이 실행되지만 저장되는 파일은 비어 어떠한 이미지 데이터가 없습니다. 누구든지 올바른 방향으로 나를 가리킬 수 있습니까? 여기 RubyMotion에 대해 알고 있지만하지 않는

답변

3

는 목표 - 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]; 
+0

이 나를 위해 아름답게했다. 트릭은 NSBitmapImageRep.alloc.initWithFocusedViewRect (rect)입니다. –

관련 문제