Bundle ImageMagick library with OS X App?

左心房为你撑大大i 提交于 2019-12-31 22:35:14

问题


I am developing an OS X application, and would like to use ImageMagick to do some image manipulation. I have noticed that the CLI ImageMagick utilities require some environment variables to work. Is it possible to bundle the ImageMagick suite of tools with my application and use them in my code?


回答1:


So here is my solution:

I bundled the OS X binary release with my project, and used an NSTask to call the binaries. You need to specify the "MAGICK_HOME" and "DYLD_LIBRARY_PATH" environment variables for NSTask to work correctly. Here is snippet of what I am using.

Note that this example is hard coded to use the "composite" command ... and uses hard coded arguments, but you can change it to whatever you like ... it is just serving 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 bundles the bulk of the entire library with your release (37MB at the moment), so it might be less than ideal for some solutions, but it is working :-)




回答2:


Possible? Yes. Many apps have done so, but it can be tedious.

NSTask allows for custom environment variables.



来源:https://stackoverflow.com/questions/4773768/bundle-imagemagick-library-with-os-x-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!