使用OS X App捆绑ImageMagick库? [英] Bundle ImageMagick library with OS X App?

查看:113
本文介绍了使用OS X App捆绑ImageMagick库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个OS X应用程序,并希望使用ImageMagick做一些图像处理。我注意到CLI ImageMagick实用程序需要一些环境变量工作。

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?

推荐答案

这里是我的解决方案:

我捆绑了 OS X二进制< a>发布与我的项目,并使用NSTask调用二进制。您需要指定MAGICK_HOME和DYLD_LIBRARY_PATH环境变量以使NSTask正常工作。这是我使用的代码片段。

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 :-)

这篇关于使用OS X App捆绑ImageMagick库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆