Moonlite <[EMAIL PROTECTED]> wrote: > I'd like to crop an image, and get another NSImage containing the selected > rectangle from the source image. > > i tried : > > - (NSImage *) imageFromRect: (NSRect) rect > { > NSImage * canvas = [[NSImage alloc] initWithSize: rect.size]; > > [ canvas setSize: rect.size ]; > [ canvas lockFocus ]; > > [self compositeToPoint: NSZeroPoint > fromRect: rect > operation: NSCompositeCopy]; > > [ canvas unlockFocus ]; > return [canvas autorelease]; > > } > > but it didn't work. Can someone tell me how to do it?
Hi, I was able to crop using NSImageRep (see below). I remember having problems with [NSImage -compositeToPoint:], but I do not remember the nature of the problem and the reason. Here is the code that works for me: In a category: @implementation NSImage (PhotoClip) - (NSImage *) imageFromRect: (NSRect) rect { NSAffineTransform * xform = [NSAffineTransform transform]; // translate reference frame to map rectangle 'rect' into first quadrant [xform translateXBy: -rect.origin.x yBy: -rect.origin.y]; NSSize canvas_size = [xform transformSize: rect.size]; NSImage * canvas = [[NSImage alloc] initWithSize: canvas_size]; [canvas lockFocus]; [xform concat]; // Get NSImageRep of image NSImageRep * rep = [self bestRepresentationForDevice: nil]; [rep drawAtPoint: NSZeroPoint]; [canvas unlockFocus]; return [canvas autorelease]; } @end I have an unfinished project with a more general method - (NSImage *) imageFromRect: (NSRect) rect scaledBy: (float) scale rotatedBy: (float) degrees (rotation +/- 90 degrees only) at http://www.vaisburd.net/PhotoClip, you might want to look at PHImage.m and PHAffineTransform.m. Hope that helps, Tima. _______________________________________________ Discuss-gnustep mailing list Discuss-gnustep@gnu.org http://lists.gnu.org/mailman/listinfo/discuss-gnustep