Implementing image pre-caching in a multi-threaded app.
Implementing image pre-caching in a multi-threaded app.
- Subject: Implementing image pre-caching in a multi-threaded app.
- From: Alexander Rauchfuss <email@hidden>
- Date: Thu, 1 Dec 2005 20:14:09 -0800
Having a spot of strangeness with a multi-threaded app.
What I am trying to do: Have separate run-loop that modifies and
then pre-caches images.
How it is currently implemented:
- Main thread sends an object with all the info about the image to
the cache run-loop.
- Cache checks to make sure that an image with the same name does not
already exist.
- If it does not, then a new image is initialized with the settings
passed to the loop.
- New image is placed in a set and pointed to by a circular array.
- Image is returned by the cache if it is called for by name.
Note: The NSImage hash: and isEqual: methods have been overridden.
So what is wrong?
Properly resized images are returned as expected, however the images
do not have anything drawn in them. So I get properly proportioned
white rectangles. This is strange because when the same image init
method is called from the main run-loop it does not have any problems.
Here are what I think may be the problem methods.
// Simplified image init method, still does not work...
- (id)initByReferencingManagedObject:(NSManagedObject *)anObject
withSize:(NSSize)baseSize;
{
self = [super initWithSize: baseSize];
if(self != nil)
{
NSRect repRect;
repRect.origin = NSZeroPoint;
repRect.size = baseSize;
NSURL *imageLocation = [NSUnarchiver unarchiveObjectWithData:
[anObject valueForKey: @"imageURL"]];
NSImageRep *toDraw = [NSImageRep imageRepWithContentsOfURL:
imageLocation];
[self lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:
NSImageInterpolationHigh];
[toDraw drawInRect: repRect];
[self unlockFocus];
[self setName: [anObject valueForKey: @"imageName"]];
}
return self;
}
// Buffer header
@interface TSSTPageBuffer : NSObject <TSSTPageBufferProto>
{
id _delegate;
// Where the images are stored for retrieval.
NSMutableSet *imageSet;
// Keeps track of where how many images are in the set and makes sure
// that old images are removed.
TSSTPageImage **circularArray;
// The size of the circular array.
int bufferSize;
// Keeps track of the current position in the circular array.
unsigned int circularCounter;
}
+ (void)launchBufferRunLoop:(NSArray *)ports;
- (TSSTPageImage *)retrievePageWithName:(id)KVCObject;
- (void)clearBuffer;
- (oneway void)bufferImages:(NSArray *)arrayOfImageObjects;
- (void)bufferSingleImage:(id)input;
@end
// Simple cache method.
- (void)bufferSingleImage:(id)input
{
TSSTPageImage *newImage, *oldImage;
// Temp var for designating where in the circular array an
// image pointer should go.
int circularArrayPosition;
// If there is not already an image in the buffer matching the
// input then an image is initialized and place in the buffer.
if(![imageSet member: [input valueForKey: @"imagePath"]])
{
// pageImageWithCurrentSettings is a simple function that finds
// the settings for an image and calls
initByReferencingManagedObject: withSize:
newImage = pageImageWithCurrentSettings(input);
// Actually places the new image in the set.
// isEqual and hash have been overridden...
NSLog(@"About to add image: %@", [input valueForKey: @"imageName"]);
[imageSet addObject: newImage];
// Calcs the position that the new image wil occupy in the
// circular array.
circularArrayPosition = circularCounter % bufferSize;
++circularCounter;
// finds the image that was previously in the
// array.
oldImage = circularArray[circularArrayPosition];
// If there actually is something at that location
// then remove that object from the set.
if(oldImage != nil)
{
[imageSet removeObject: oldImage];
}
// place the new image in the circular array.
circularArray[circularArrayPosition] = newImage;
}
}
Previously I had not bothered with a run-loop and had just detached
the proper method each time I needed to update the cache. Worked
fine in that case too.
Anyone have any idea what I might be falling afoul of?
Also this is my first time posting to this list, so if you have any
etiquette/formating suggestions don't hesitate to tell me.
Alexander Rauchfuss
email@hidden
_______________________________________________
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