|Oct 26, 2009, 08:42 AM||#1|
Cocoa/Objective-C: crop and save an image
I'm a Java/PHP programmer, and I've started programming in Objective-C for Mac OSX.
I've understood the MVC pattern for programming user interfaces with Cocoa, but I'm very confused about the SDK to manage filesystem, images, and so on...
I've a quite stupid problem but I cannot find an easy solution.
What I need is:
1. Load an image from a file
2. Check the size (width and height) and do some stuffs...
3. Crop this image in squares
4. Save each square in a new image file (PNG).
I cannot understand how to achieve point 3 and what kind of object I have to use, NSImage? CIImage? CGImage?
Any suggestment will be appreciated.
Thanks a lot.
Last edited by lodevalm; Oct 26, 2009 at 09:00 AM.
|Oct 26, 2009, 09:59 AM||#2|
NSImage will probably work fine. Pseudo-code for what you want to do is:
1) Create a new NSImage of the size you want the output to be
2) Lock focus for drawing on that image
3) Draw the image you want crop into the new image
4) Unlock focus
5) Save the PNG representation.
5) is a bit more complex. I would suggest that you can use addRepresentation: and add a NSBitmapImageRep (or check if there is one first). Once you have an NSImageBitmapRep you can use representationUsingTyperoperties: with NSPNGFileType and nil properties to get PNG data to write to a file.
Sponsor me to cycle 100Km round London in the dark
|Oct 27, 2009, 06:51 AM||#3|
your help put me in the right way :-)
so, here is the first version of the code:
NSImage *source = [[NSImage alloc]initWithContentsOfFile:@"/Users/daniele/Pictures/lda/lodevalm01.jpg"]; NSImage *target = [[NSImage alloc]initWithSize:NSMakeSize(128,128)]; [target lockFocus]; [source drawInRect:NSMakeRect(0,0,128,128) fromRect:NSMakeRect(0,0,128,128) operation:NSCompositeCopy fraction:1.0]; [target unlockFocus]; NSBitmapImageRep *bits = [[target representations] objectAtIndex: 0]; NSData *data = [bits representationUsingType: NSPNGFileType properties: nil]; [data writeToFile: @"/Users/daniele/Pictures/lda/lodevalm01_crop.png" atomically: NO];
I'm not sure, because I cannot understand exactly the messages from the debugger, but I've got an error calling representationUsingType, the message is:
Uncaught exception: <NSInvalidArgumentException> *** -[NSCachedImageRep representationUsingTyperoperties:]: selector not recognized [self = 0x312b80]
Could you please help me again in understand this part?
Just in case, as additional info, I'm using version 2.5 of XCode.
(Sorry for the english, I hope it's clear...)
|Oct 27, 2009, 07:52 AM||#4|
You have made an unsafe assumption. You assume that the first representation is an NSBitmapImageRep. This may or may not be the case. In your case it is not: it's an NSCachedImageRep. You need to iterate through the representations until you find an NSBitmapImageRep. If you don't find one create one and add it to the NSImage.
Sponsor me to cycle 100Km round London in the dark
|Oct 27, 2009, 09:53 AM||#5|
Ok, now I did it :-)
This code works!
I've created a NSBitmapImageRep and added it to the target representations.
//Get the source image from file NSImage *source = [[NSImage alloc]initWithContentsOfFile:@"/Users/daniele/Pictures/lda/lodevalm02.jpg"]; //Init target image NSImage *target = [[NSImage alloc]initWithSize:NSMakeSize(128,128)]; //start drawing on target [target lockFocus]; //draw the portion of the source image on target image [source drawInRect:NSMakeRect(0,0,128,128) fromRect:NSMakeRect(0,0,128,128) operation:NSCompositeCopy fraction:1.0]; //end drawing [target unlockFocus]; //create a NSBitmapImageRep NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:[target TIFFRepresentation]]; //add the NSBitmapImage to the representation list of the target [target addRepresentation:bmpImageRep]; //get the data from the representation NSData *data = [bmpImageRep representationUsingType: NSPNGFileType properties: nil]; //write the data to a file [data writeToFile: @"/Users/daniele/Pictures/lda/lodevalm01_crop.png" atomically: NO];
This code will be a function in a utility class, so, after doing this stuffs, I need to release every object I used?
In this case the source and target image, because other objects are pointers. Is it correct?
|Oct 27, 2009, 10:11 AM||#6|
data should be autoreleased since representationUsingType does not contain the words alloc or new, so it should get release sent to it the next time an autorelease pool is released. If you're calling this method a lot in a loop, you may want to set up a new autorelease pool around the loop and release it afterwards so all of these things will get released immediately.
If you have not already, read:
|Nov 3, 2009, 09:23 AM||#7|
at the moment, the app only do the crop task as helper in a project, I have no time to go deeper in memory management, so, it works with your suggests...and it's enough! :-)
Inside a for loop I alloc an NSImage (the cropped image), I use it with the NSBitmapImageRep and then I dispose those two objects.
After the loop I dispose the target image.
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Cocoa Touch and Objective C||phalgun27||iPhone/iPad Programming||7||Aug 1, 2013 07:09 PM|
|Basic Objective-C Cocoa questions?||cpuin||Mac Programming||8||Jun 3, 2013 07:10 PM|
|Can I make Objective-C/Cocoa command-line text-mode applications?||printz||Mac Programming||3||May 2, 2013 08:01 PM|
|BecomeAnXcoder : A free book for starting with Cocoa using Objective-C||msaljaber||iPhone/iPad Programming||0||Feb 7, 2013 05:30 AM|
|WebView in Cocoa (Objective-C)||tr!pf!3||Mac Programming||0||Aug 28, 2012 09:48 AM|
All times are GMT -5. The time now is 06:58 AM.