Skip navigation.
 
mlRe: how to get a sub piece of an NSImage or make a new NSImage that is a subset of an existing one
FROM : Jonathan Grynspan
DATE : Sat Aug 12 07:28:32 2006

Try the following:

NSImage *partOfImage(NSImage *input, NSRect targetRect) {
   if (input) {
       NSImage *output = [[NSImage alloc] initWithSize: targetRect.size];
       [output lockFocus];
       [input
           drawAtPoint:    NSZeroPoint
           fromRect:    targetRect
           operation:    NSCompositeCopy
           fraction:        1.0f];
       [output unlockFocus];
       return [output autorelease];
   } else {
       return nil;
   }
}

-Jonathan Grynspan

On 12-Aug-06, at 1:17 AM, Chad Leigh wrote:

> I have a Cocoa app and I get a PDF image over the internet that I 
> store in an NSImage using
>
>    label = [[NSImage alloc] initWithData:finalImage];
>
>
> The image previously was a PNG image and was exactly the image 
> needed.  Now that they pass a PDF image, they pass an image that is 
> an image of a complete piece of paper with the image I am 
> interested in placed on the upper half of this virtual paper (the 
> rest is just blank).  The image is always in the same place and the 
> same size so I can determine a Rect that will always work to select 
> it.  I want to extract this image out of the PDF and create a new 
> NSImage (or overwrite the old one, I don't care) that I can use 
> later on to print in my own existing print routine and to display 
> thumbnails of it.
>
> I do not see how to go about it.  Most of the draw/composite 
> routines I see in NSImage rely on an existing userspace to draw 
> into.  I want to "draw" into a new NSImage.  My brain is having a 
> hard time adjusting to think about how to do this.
>
> Ideally I want something like
>
>     NSImage *newImage = [[NSImage alloc] initFromImage:oldImage 
> usingRect:targetRect];
>
> I expect what I want to do is very easy but if someone could point 
> me on the way I should be exploring I would appreciate it.
>
>
> Thanks
> Chad
>
> _______________________________________________
> MacOSX-dev mailing list
> <email_removed>
> http://www.omnigroup.com/mailman/listinfo/macosx-dev