NSThread/NSImage problem
NSThread/NSImage problem
- Subject: NSThread/NSImage problem
- From: Nick Morris <email@hidden>
- Date: Sun, 23 Sep 2001 16:28:33 +0100
Hi,
I am having a problem with NSImage in NSThread.
I have written a method that works very well when not called in a
separate thread. It checks out with MallocDebug when in the main
thread. As I understand the situation NSImage is thread safe (the image
is not being accessed in the other thread).
The method uses a for loop to load a series of different jpeg files (one
at a time), resizes the picture, and saves it. The picture is then
resized again and then re-saved to a new file (code below).
Some times the method works fine. However, every so often (i.e. more
often than it works!) it crashes with signal 11 (SIGSEGV). This usually
occurs at an unlockFocus on the getState ([thePicture unlockFocus]).
This can be the first or second unlock, and the loop always executes at
least once.
Any thoughts and help greatly appreciated.
Nick
The code:
for (i = 0; i < (arrayCount); i++)
{
NSImage *thePicture = [NSImage alloc];
NSData *thePictureData = [NSData alloc];
NSData *thePictureData2 = [NSData alloc];
NSBitmapImageRep *theBitMapToBeSaved = [NSBitmapImageRep alloc];
NSBitmapImageRep *theBitMapToBeSaved2 = [NSBitmapImageRep alloc];
// Load and deal with picture
[thePicture initWithContentsOfFile:sourcePath]; // Load the file
in to NSImage thePicture
[thePicture setScalesWhenResized:YES]; // Allow rescale
newSizeForPict.height = ([thePicture size].height) * 0.5; //
Calculate new height
newSizeForPict.width = ([thePicture size].width) * 0.5; //
Calculate new width
//Change picture size
[thePicture lockFocus]; // Lock focus (needed to produce change)
[thePicture setSize:newSizeForPict]; // Change size
[thePicture unlockFocus]; // Unlock focus when finished
// Save re-sized picture
thePictureData = [thePicture2 TIFFRepresentation];
theBitMapToBeSaved = [NSBitmapImageRep
imageRepWith
Data:thePictureData];
[[theBitMapToBeSaved representationUsingType:NSJPEGFileType
properties:theFileSaveDictionary] writeToFile:saveFilePath
atomically:NO];
newSizeForPict.height = ([thePicture size].height) * 0.25; //
Calculate new height
newSizeForPict.width = ([thePicture size].width) * 0.25; //
Calculate new width
//Change picture size
[thePicture lockFocus]; // Lock focus (needed to produce change)
[thePicture setSize:newSizeForPict]; // Change size
[thePicture unlockFocus]; // Unlock focus when finished
// Save main re-sized picture
thePictureData2 = [thePicture TIFFRepresentation];
theBitMapToBeSaved2 = [NSBitmapImageRep
imageRepWith
Data:thePictureData2];
[[theBitMapToBeSaved2 representationUsingType:NSJPEGFileType
properties:theFileSaveDictionary] writeToFile:saveFilePath
atomically:NO];
thePicture = nil;
thePictureData = nil;
thePictureData2 = nil;
[thePicture release];
[thePictureData release];
[thePictureData2 release];
}