Bundle ImageMagick library with OS X App?

I am developing an OS X application and would like to use ImageMagick to do some image manipulation. I noticed that ImageMagick CLI utilities require some environment variables. Can I link the ImageMagick toolkit to my application and use them in my code?

+7
source share
2 answers

So here is my solution:

I associated OS X binary with my project and used NSTask to invoke binary files. You need to specify the environment variables "MAGICK_HOME" and "DYLD_LIBRARY_PATH" for NSTask to work. Here is a snippet of what I'm using.

Note that this example is hardcoded to use the compound command ... and uses hardcoded arguments, but you can change it to whatever you like ... it just serves as a proof of concept.

-(id)init { if ([super init]) { NSString* bundlePath = [[NSBundle mainBundle] bundlePath]; NSString* imageMagickPath = [bundlePath stringByAppendingPathComponent:@"/Contents/Resources/ImageMagick"]; NSString* imageMagickLibraryPath = [imageMagickPath stringByAppendingPathComponent:@"/lib"]; MAGICK_HOME = imageMagickPath; DYLD_LIBRARY_PATH = imageMagickLibraryPath; } return self; } -(void)composite { NSTask *task = [[NSTask alloc] init]; // the ImageMagick library needs these two environment variables. NSMutableDictionary* environment = [[NSMutableDictionary alloc] init]; [environment setValue:MAGICK_HOME forKey:@"MAGICK_HOME"]; [environment setValue:DYLD_LIBRARY_PATH forKey:@"DYLD_LIBRARY_PATH"]; // helper function from // http://www.karelia.com/cocoa_legacy/Foundation_Categories/NSFileManager__Get_.m NSString* pwd = [Helpers pathFromUserLibraryPath:@"MyApp"]; // executable binary path NSString* exe = [MAGICK_HOME stringByAppendingPathComponent:@"/bin/composite"]; [task setEnvironment:environment]; [task setCurrentDirectoryPath:pwd]; // pwd [task setLaunchPath:exe]; // the path to composite binary // these are just example arguments [task setArguments:[NSArray arrayWithObjects: @"-gravity", @"center", @"stupid hat.png", @"IDR663.gif", @"bla.png", nil]]; [task launch]; [task waitUntilExit]; } 

This solution combines the bulk of the entire library with your version (37 MB at the moment), so for some solutions it may be less than ideal, but it works :-)

+10
source

Possible? Yes. Many applications have done this, but it can be tedious.

NSTask allows you to configure environment variables.

+2
source

All Articles