NSImage issue in 10.6
NSImage issue in 10.6
- Subject: NSImage issue in 10.6
- From: Amy Gibbs <email@hidden>
- Date: Mon, 03 Dec 2012 13:02:49 +0000
I have the following code that generates images. It works perfectly in
10.5 but doesn't in 10.6. in 10.6 the images are laid on in the right
place,
- (IBAction)generateKitImages:(id)sender;
{
NSObject *kit;
kit = [[kits selectedObjects] objectAtIndex:0];
NSString* fileName = [[kit valueForKey:@"kitAbbr"]
stringByAppendingString:@".jpg"];
NSString* thumbnail = [[kit valueForKey:@"kitAbbr"]
stringByAppendingString:@"TN.jpg"];
NSString* kitimagePath = [[[[self applicationSupportFolder]
stringByAppendingPathComponent:@"images"]
stringByAppendingPathComponent:@"kit"]
stringByAppendingPathComponent:fileName];
NSString* desktopPath = [[[[NSHomeDirectory()
stringByAppendingPathComponent:@"Desktop"]
stringByAppendingPathComponent:@"Xcart Upload"]
stringByAppendingPathComponent
:@"images"]stringByAppendingPathComponent:fileName];
NSString* kitThumbnailPath = [[[[self applicationSupportFolder]
stringByAppendingPathComponent:@"images"]
stringByAppendingPathComponent:@"kit"]
stringByAppendingPathComponent:thumbnail];
NSString* desktopThumbnailPath = [[[[NSHomeDirectory()
stringByAppendingPathComponent:@"Desktop"]
stringByAppendingPathComponent:@"Xcart Upload"]
stringByAppendingPathComponent
:@"images"]stringByAppendingPathComponent:thumbnail];
NSLog(@"Desktop path is: %@",desktopPath);
//select all images for kit
NSArray* kitImages = [kit
valueForKeyPath:@"kitItems.kitItemProduct.productImage"];
int ki = [kitImages count];
NSLog(@"Number of images: %i",ki);
//NSLog(@"Kit Image Path: %@", kitimagePath);
//create new image 300px square
NSImage *targetImage;
NSSize targetSize = NSMakeSize (600, 600);
targetImage = [[NSImage alloc] initWithSize:targetSize];
//set coordinates to x,y -> 0,0 to start
float x = 0;
float y = 0;
//change view depending on image count
if (ki>9){
//greater than 8 images
//background images to fill space
float xb = 600;
float yb = 600;
//for each image
NSEnumerator *imageLoop = [kitImages objectEnumerator];
NSString *imgPath;
while ((imgPath = [imageLoop nextObject])) {
NSImage *img = [[NSImage alloc]initWithContentsOfFile:imgPath];
//apply image to view
[targetImage lockFocus];
[img drawInRect:NSMakeRect(x,y,xb,yb) fromRect:NSMakeRect(0,0,0,0)
operation:NSCompositeCopy fraction:1];
xb = 151;
yb = 151;
//set new coordinates
x = x+150;
//if coordinates are too wide, start new row - if x>300, reset x to
0 and add 100 to y
if( x > (float)599 ){
x = x-600;
y = y+150;
//NSLog(@"x is greater than 300");
}
}
}
else if (ki>6) {
NSLog(@"7-10 images");
//background images to fill space
float xb = 600;
float yb = 600;
//for each image
NSEnumerator *imageLoop = [kitImages objectEnumerator];
NSString *imgPath;
while ((imgPath = [imageLoop nextObject])) {
NSImage *img = [[NSImage alloc]initWithContentsOfFile:imgPath];
//apply image to view
[targetImage lockFocus];
[img drawInRect:NSMakeRect(x,y,xb,yb) fromRect:NSMakeRect(0,0,0,0)
operation:NSCompositeCopy fraction:1];
xb = 201;
yb = 151;
//set new coordinates
x = x+200;
//if coordinates are too wide, start new row - if x>300, reset x to
0 and add 100 to y
if( x > (float)599 ){
x = x-600;
y = y+150;
//NSLog(@"x is greater than 300");
}
}
}
else {
NSLog(@"up to 6 images");
//background images to fill space
float xb = 600;
float yb = 600;
//for each image
NSEnumerator *imageLoop = [kitImages objectEnumerator];
NSString *imgPath;
while ((imgPath = [imageLoop nextObject])) {
NSImage *img = [[NSImage alloc]initWithContentsOfFile:imgPath];
//apply image to view
[targetImage lockFocus];
[img drawInRect:NSMakeRect(x,y,xb,yb) fromRect:NSMakeRect(0,0,0,0)
operation:NSCompositeCopy fraction:1];
xb = 201;
yb = 226;
//set new coordinates
x = x+200;
//if coordinates are too wide, start new row - if x>300, reset x to
0 and add 100 to y
if( x > (float)599 ){
x = x-600;
y = y+225;
//NSLog(@"x is greater than 300");
}
}
}
//apply kit logo to view
NSString *kitLogoFilePath = [kit valueForKeyPath:@"kitClub.clubLogo"];
NSImage *kitLogoImg = [[NSImage
alloc]initWithContentsOfFile:kitLogoFilePath];
[kitLogoImg drawInRect:NSMakeRect(375,75,150,150)
fromRect:NSMakeRect(0,0,0,0) operation:NSCompositeCopy fraction:1];
//apply wtc logo to view
//apply kit date (text) to view
//apply stroke arund edge
NSPoint origin = { 0,0 };
NSRect rect;
rect.origin = origin;
rect.size.width = 600;
rect.size.height = 600;
NSBezierPath * path;
path = [NSBezierPath bezierPathWithRect:rect];
[path setLineWidth:5];
[[NSColor colorWithDeviceRed:(float)0.624
green:(float)0.694
blue:(float)0.471
alpha:(float)1.0] set];
[path stroke];
//save files out
[targetImage unlockFocus];
//create a NSBitmapImageRep
NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:
[targetImage TIFFRepresentation]];
//add the NSBitmapImage to the representation list of the target
[targetImage addRepresentation:bmpImageRep];
//get the data from the representation
NSData *data = [bmpImageRep representationUsingType: NSJPEGFileType
properties: nil];
//write the data to a file
[data writeToFile: kitimagePath
atomically: NO];
[[ NSFileManager defaultManager ] copyItemAtPath:kitimagePath
toPath:desktopPath error:nil];
//make thumbnail image
NSImage *thumbImage;
NSImage *sourceImg = [[NSImage
alloc]initWithContentsOfFile:kitimagePath];
NSSize thumbSize = NSMakeSize (200, 200);
thumbImage = [[NSImage alloc] initWithSize:thumbSize];
[thumbImage lockFocus];
[sourceImg drawInRect:NSMakeRect(0,0,200,200)
fromRect:NSMakeRect(0,0,0,0) operation:NSCompositeCopy fraction:1];
[thumbImage unlockFocus];
//create a NSBitmapImageRep
NSBitmapImageRep *thumbbmpImageRep = [[NSBitmapImageRep
alloc]initWithData:[thumbImage TIFFRepresentation]];
//add the NSBitmapImage to the representation list of the target
[thumbImage addRepresentation:thumbbmpImageRep];
//get the data from the representation
NSData *thumbdata = [thumbbmpImageRep representationUsingType:
NSJPEGFileType
properties: nil];
[thumbdata writeToFile: kitThumbnailPath
atomically: NO];
[[ NSFileManager defaultManager ] copyItemAtPath:kitThumbnailPath
toPath:desktopThumbnailPath error:nil];
//link images to kit
[kit setValue:kitimagePath forKey:@"kitImage"];
[kit setValue:kitThumbnailPath forKey:@"kitThumbnail"];
//cleanup
[bmpImageRep release];
//[img release];
[kitLogoImg release];
[targetImage release];
[thumbImage release];
}
Willow Tree Crafts
email@hidden
www.willowtreecrafts.co.uk
Many Thanks
Amy Gibbs
Willow Tree Crafts
www.willowtreecrafts.co.uk
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden