objective-c - 应用程序包中的 ImageMagick

标签 objective-c xcode macos cocoa

我找到了question (link)女巫和我的问题非常相似。以及 answer 中的代码看起来是我一直在寻找的东西:

-(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];
}

但是当我尝试使用它时,我遇到了 6 个错误:

Use of undeclared identifier 'MAGICK_HOME'
Use of undeclared identifier 'DYLD_LIBRARY_PATH'
Use of undeclared identifier 'MAGICK_HOME'
Use of undeclared identifier 'DYLD_LIBRARY_PATH'
Use of undeclared identifier 'Helpers'
Use of undeclared identifier 'MAGICK_HOME'

enter image description here

我该如何解决这个问题?

最佳答案

要使用您链接的代码,可能您只需将它们声明为全局变量即可。

NSString * MAGICK_HOME = nil;
NSString * DYLD_LIBRARY_PATH = nil;

@implementation ...

Helpers 替换为 [self class],并从链接源添加函数:

/*
    NSFileManager: Get the path within the user's Library directory
    Original Source: <http://cocoa.karelia.com/Foundation_Categories/NSFileManager__Get_.m>
    (See copyright notice at <http://cocoa.karelia.com>)
*/

/*" Return the path in the user library path of the given sub-path.  In other words, if given inSubPath is "foo", the path returned will be /Users/myUser/Library/foo
"*/
-  (NSString *) pathFromUserLibraryPath:(NSString *)inSubPath
{
    NSArray *domains = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
    NSString *baseDir= [domains objectAtIndex:0];
    NSString *result = [baseDir stringByAppendingPathComponent:inSubPath];
    return result;
}

关于objective-c - 应用程序包中的 ImageMagick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11651512/

相关文章:

objective-c - 即使该应用被终止,仍继续使用NSURLSession下载

iphone - iOS - pngcrush 差异

iphone - 我在 Xcode 4.5.2 中收到 'deprecated' 警告

macos - 使用哪个 Cocoa 控件来显示向下钻取表?

macos - 在我的首选项 Pane 中定义 anchor 名称

同名的 Objective-C 类别

ios - 在 iOS7 中取消交互式弹出手势时如何将数据传递给子 Controller ?

ios - UIImage 返回错误的宽度

ios xcode : checking live updates using a . 网络 API

java - 10.9.3 Android Studio 之后的 Mac 操作系统无法运行