2

我正在尝试使用此代码在应用程序包中获取我的 ImageMagick 库,但这非常复杂:

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

声明DYLD_LIBRARY_PATHMAGICK_HOME标识符(已解决)

更新:

但是当我尝试构建和运行它时,我的应用程序崩溃了。崩溃于:[task launch];
控制台消息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'working directory doesn't exist.'

我该如何解决当前的问题?

更新 2:

当前代码:

- (id)initWithCoder:(NSCoder *)coder
{
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
NSString* imageMagickPath = [bundlePath stringByAppendingPathComponent:@"/Contents/Resources/ImageMagick"];
NSString* imageMagickLibraryPath = [imageMagickPath stringByAppendingPathComponent:@"/lib"];

MAGICK_HOME = imageMagickPath;
DYLD_LIBRARY_PATH = imageMagickLibraryPath;
[self composite];
}

-(void)composite
{
    NSTask *task = [[NSTask alloc] init];
    
    NSMutableDictionary* environment = [[NSMutableDictionary alloc] init];
    [environment setValue:MAGICK_HOME forKey:@"MAGICK_HOME"];
    [environment setValue:DYLD_LIBRARY_PATH forKey:@"DYLD_LIBRARY_PATH"];
    
    NSString* loc = [[NSString stringWithFormat:@"%@", MAGICK_HOME] retain];
    NSString* exe = MAGICK_HOME;
    
    [task setEnvironment:environment];
    NSString* pwd = @"/opt/local/lib/";
    [task setCurrentDirectoryPath:pwd];
    [task setLaunchPath:loc];
    NSLog(@"%@", loc);
    NSLog(@"%@", pwd);
    [task setArguments:[NSArray arrayWithObjects: @"-gravity", @"center", @"stupid hat.png", @"IDR663.gif", @"bla.png", nil]];
    [task launch];
    [task waitUntilExit];
}

和当前错误(在控制台中):

*** NSTask: Task create for path '/Users/development/Library/Developer/Xcode/DerivedData/OGL-cahltqazoqxhrthkxztsqyvvodge/Build/Products/Debug/OGL.app/Contents/Resources/ImageMagick' failed: 22, "Invalid argument".  Terminating temporary process.
4

1 回答 1

2

[task setLaunchPath:...]必须使用可执行二进制文件的路径调用。在您的“更新 2”代码中,您使用目录路径调用它。

于 2012-07-26T15:16:37.957 回答