Re: Can't cache image
Re: Can't cache image
- Subject: Re: Can't cache image
- From: Brock Brandenberg <email@hidden>
- Date: Fri, 21 Mar 2003 11:22:03 -0600
Sorry. No matter how many times I do this, I still occasionally reply with
the default digest subject which screws up the archives thread.
-------
Hi Ben.
>
I keep getting the error "Can't cache image" running the following code:
>
>
NSImage *myNewImage = [[NSImage alloc] initWithSize:NSMakeSize([image1
>
size].width , [image1 size].height)];
>
[myNewImage setCacheMode:NSImageCacheNever];
>
[myNewImage lockFocus];
>
>
It gives me the error on the lockFocus line. I am not sure why, or what I
>
can do to get it to work as it should. Here is the greater look of my code,
>
so you can get an idea of what I am trying to do:
The error is because you have told the NSImage to never cache. Because you
are telling it not to cache, it's responding that it can't create the
offscreen window buffer in which to capture and cache your subsequent
drawing after the lockFocus.
If you're trying to capture your drawing as a bitmap, you do this as covered
under "Caching Representations" in "Drawing and Imaging". You can then use
the NSBitmapImageRep for whatever you want:
NSImage* image = [[NSImage alloc] initWithSize:size];
[image lockFocus];
// draw draw draw
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc]
initWithFocusedViewRect:
NSMakeRect(0,0,size.width,size.height)];
[image unlockFocus];
Though not documented well at all (IMHO), there is a difference between an
NSImage cache and an NSCachedImageRep. NSImage appears to keep an off-screen
window buffer from which it can draw when the conditions are not sufficient
to call upon one of its image reps to draw. This off-screen window buffer is
not an image rep, and you will not see it under the representations of the
NSImage. But, you can see that it exists if you create an NSPDFImageRep, add
it to a new NSImage, make sure that the NSImage is set to
NSImageCacheDefault, then draw the NSImage to the screen at a
frame-to-bounds ratio that causes it to scale. It will be pixelated,
illustrating that the PDF was rendered once to the cache, and then pulled
from the cache to draw to the screen context. If it is written to a printing
context, the NSImage will call upon the PDF image rep to draw itself again
instead of pulling from the cache. If you want to prevent the NSImage from
creating an NSCachedImageRep and discarding the original image rep when it
pulls in your data, set the NSImage setDataRetained: to YES sometime before
the NSImage is called upon to draw.
There's a little info more on NSImage caching under the MacOS X 10.2 AppKit
release notes.
Brock Brandenberg
----- industrial design @ www.bergdesign.com ------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.