PDA

View Full Version : Saving image output to file in cocoa app




FelixMC
Jan 30, 2008, 04:43 PM
Ok, so I'm more of a Html/Javascript/CSS/PHP/Ruby/Applescript kind of person, and I really stink at Java, Cocoa and Objective-C...:D
But I'm attempting to get better, so I'm occasionally building an app here and there to help clean up my Mac or just solve problems I can't with other apps:p..
Right now to make my web developing life easier, I'm "trying" to build a nav bar generator, which generates both code and graphics. I went ahead and build all my application "engine" and code and graphics generators in Quartz Composer. Turns out it's a great tool for making "rules", besides just cool graphics. So, the application as it is now, works pretty well, with the few exceptions of a few bugs here and there, and of course I could add a slicker interface to it.
Now heres my problem. The app generates different graphics, according to some user controlled inputs and settings. There is a NSImageView object(?) binded to a image output from a QC patch controller. I would like the user to be able to save the image to a usable image file, preferably in .png format, but any other format supporting an alpha channel and recognized by popular browsers without need of an external plug-in I think will do. :)
So, what's the easiest way to do this..? I'll take anything you guys have to offer including alternatives or links or wtv :D
BTW, if this helps, I'm running Tiger, and I'm using the Tiger SDK and Xcode 2.5. Thanks in advance :D



robbieduncan
Jan 30, 2008, 05:35 PM
PNG is a little more difficult, TIFF is easy: just ask for the TIFF representation (http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/Reference/Reference.html#//apple_ref/occ/instm/NSImage/TIFFRepresentation) and write it to disk.

To write to a PNG you need to get a NSBitmapImageRep (http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/Reference/Reference.html) (you should be able to create one from the TIFFRepresentation as above) then ask it for the representation of the file type you want with representationUsingType:properties: (http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/Reference/Reference.html#//apple_ref/occ/instm/NSBitmapImageRep/representationUsingType:properties:).

FelixMC
Jan 30, 2008, 09:33 PM
Thanks for the help &link :)
It all makes sense, to a certain extent..:rolleyes:
See, part of the fact that I stink at Cocoa and Objective-C, is because I never really got to really learn them. I just improvised on some things I learned from guides and tutorials..
So, according to that link I need (or I think I do) a NSImage, a NSImage header and ".m" (?) file. So I initiated a NSImage (took me a while to figure that out:p). Than I created the header and ".m" file in Xcode and added the code in the header file: (NSData *)TIFFRepresentation
Now I suppose I should be linking he QC image output to the NSImage somehow..? Or the NSImageView to the NSImage..?
And how to I retrieve the response from the NSData? I wasn't able to find an NSData class in Interface Builder. Will I need to initiate a Controller?
Any further help, dummy slaps or links to little yellow books entitled "Cocoa for Dummies" would be greatly appreciated..:D

robbieduncan
Jan 31, 2008, 04:00 AM
If you post the project (without the compiled bits) we might be able to help. At this stage I'm struggling to understand what you've done...

FelixMC
Jan 31, 2008, 03:48 PM
Ok, I will strip off all unnecessary things from the Xcode project for the sake of saving space and put it on here... but I've got a little problem....
Here (http://forums.macrumors.com/showthread.php?p=4868889#post4868889)it is...