• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Caching an NSCachedImageRep?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Caching an NSCachedImageRep?


  • Subject: Caching an NSCachedImageRep?
  • From: Dominic <email@hidden>
  • Date: Sat, 7 May 2005 11:23:49 -0700

Hi all,

I'm trying to cache large image files to display them quickly. The
trouble is that the first time I draw the cached image, there is a
perceptible delay. The second and subsequent times I have my NSView
draw, display is instant. This mystifies me.

Relevant snippets of code below:

orig = [[NSImage alloc] initByReferencingFile:hugeImageFile];
NSSize oldSize = [orig size];
NSSize smallerSize = [myView bounds].size;

// scale down image
result = [[NSImage alloc] initWithSize:smallerSize];
[result lockFocus];
[orig drawInRect:NSMakeRect(0,0,smallerSize.width,smallerSize.height)
		fromRect:NSMakeRect(0,0,oldSize.width,oldSize.height)
	   operation:NSCompositeSourceOver fraction:1.0];
[result unlockFocus];

[myView setImage:result];

// later, in my view's drawRect...

- (void)drawRect:(NSRect)rect {
	NSRect sourceRect;
	sourceRect.origin = NSZeroPoint;
	sourceRect.size = [image size];
	// note that [image size] is equal to [self bounds].size, from above
	[image drawInRect:[self bounds] fromRect:sourceRect
			operation:NSCompositeSourceOver fraction:1.0];
}

Now the first time myView has to display the image, it has to stop and
think for a while. Not that long, but enough to be annoying. But if I
switch to another image ([myView setImage:image2]),wait for it to
true, and then switch back, my first image draws instantly. But why?
My "result" is exactly the same size as the view, and (according to
the debugger) it's an NSCachedImageRep, so it should be equally fast
every time, right?

While I'm on the topic, is there a good explanation of how NSImage
works somewhere? Things like caching, sizes, compositing/drawing, and
what order everything happens in. Apple's docs are a bit minimalistic
for me.

Thanks,
-Dominic
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: Tiger, Interface Builder and NSScrollViews?
  • Next by Date: Re: Tiger, Interface Builder and NSScrollViews?
  • Previous by thread: Re: Tiger, Interface Builder and NSScrollViews?
  • Next by thread: Category conflicts with Tiger
  • Index(es):
    • Date
    • Thread