Nip*_*rus 7 cocoa imagemagick objective-c
我正在开发OS X应用程序,并希望使用ImageMagick进行一些图像处理.我注意到CLI ImageMagick实用程序需要一些环境变量才能工作.是否可以将ImageMagick工具套件与我的应用程序捆绑在一起并在我的代码中使用它们?
Nip*_*rus 10
所以这是我的解决方案:
我将OS X二进制版本与我的项目捆绑在一起,并使用NSTask来调用二进制文件.您需要为NSTask指定"MAGICK_HOME"和"DYLD_LIBRARY_PATH"环境变量才能正常工作.这是我正在使用的片段.
请注意,此示例是硬编码以使用"复合"命令...并使用硬编码参数,但您可以将其更改为您喜欢的任何内容...它只是作为概念证明.
-(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];
}
Run Code Online (Sandbox Code Playgroud)
此解决方案将整个库的大部分与您的版本捆绑在一起(目前为37MB),因此对于某些解决方案可能不太理想,但它正在运行:-)