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 : Chad Leigh
DATE : Sat Aug 12 09:47:51 2006

On Aug 12, 2006, at 12:01 AM, Chad Leigh wrote:

>
> On Aug 11, 2006, at 11:43 PM, Jonathan Grynspan wrote:
>

>> You can find more information on Cocoa's graphics model at http://
>> developer.apple.com/documentation/Cocoa/Conceptual/
>> CocoaDrawingGuide/index.html . Of particular interest to you may 
>> be the sections on Graphics Contexts and Images.

>
> I am reading that now, thanks.
>
> Unfortunately my new image is a cached screen representation and 
> not the hi-res PDF that it was before.  And it appears from the 
> CocoaDrawingGuide docs under "Creating Graphics Contexts" that you 
> cannot create a new PDF image using the code below.  It says :-
> ( "Important:  You cannot create a viable graphics context for PDF 
> or PostScript canvases using the graphicsContextWithAttributes: 
> method. You must go through the Cocoa Printing system instead." 
> That however, is what I need to do, I think.


It looks like something like this works:

        NSImageView *imageView = [[[NSImageView alloc] 
initWithFrame:labelPDFRect] autorelease];
        [imageView setImage:newLabel];
        [imageView beginDocument];
        NSMutableData    *imageData = [NSMutableData data];
        NSPrintOperation *pdfOp = [NSPrintOperation 
PDFOperationWithView:imageView insideRect:imageRect toData:imageData];
        [pdfOp runOperation];
        label = [[NSImage alloc] initWithData:imageData];
   //     [pdfOp cleanUpOperation];
        [imageView endDocument];


I get my image and it is still a PDF based image by looking at the 
single NSImageRep (NSPDFImageRep) that it contains, though I have not 
yet gotten my printing code to spit out the image as I seem to have 
screwed something up and all I get now is a gray box the size of the 
page (without margins) so the paper comes out with a gray box with 
normal white  margin.  This happens even when I use my PNG image code 
so I screwed something up somewhere.

Chad


>
> Thanks
> Chad
>

>>
>> -Jonathan Grynspan
>>
>> On 12-Aug-06, at 1:34 AM, Chad Leigh wrote:
>>

>>>
>>> On Aug 11, 2006, at 11:28 PM, Jonathan Grynspan wrote:
>>>

>>>> 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;
>>>>     }
>>>> }

>>>
>>> Thanks, I will.  I've been reading the lockFocus and the 
>>> drawAtPoint stuff since I posted the first request, trying to put 
>>> my brain around it.  This helps immensely!
>>>
>>> Thanks
>>> Chad
>>>

>>>>
>>>> -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

>>>>
>>>> _______________________________________________
>>>> MacOSX-dev mailing list
>>>> <email_removed>
>>>> http://www.omnigroup.com/mailman/listinfo/macosx-dev

>>>
>>> _______________________________________________
>>> MacOSX-dev mailing list
>>> <email_removed>
>>> http://www.omnigroup.com/mailman/listinfo/macosx-dev

>>
>> _______________________________________________
>> MacOSX-dev mailing list
>> <email_removed>
>> http://www.omnigroup.com/mailman/listinfo/macosx-dev

>
> _______________________________________________
> MacOSX-dev mailing list
> <email_removed>
> http://www.omnigroup.com/mailman/listinfo/macosx-dev