objective-c - NSTask 在 xCode 问题中启动

标签 objective-c xcode macos cocoa

我正在尝试使用此代码在应用程序包中获取我的 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 标识符(已解决)

更新:

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

*** 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.

最佳答案

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

关于objective-c - NSTask 在 xCode 问题中启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11663505/

相关文章:

objective-c - 如何将Leap Motion Controller 集成到Mac应用程序中?

ios - 为什么我的 UI 会在我明确离开返回主线程时更新

ios - UIViewController 在模式呈现后更改尺寸

ios - 一起点击或按下时在两个 UIlabel 之间画一条线

cocoa - 在XCode中,当您在IB中而不是在代码中创建类的实例时,实例对象的名称是什么?

ios - 在 XCode 6.1 中工作的代码在 XCode 6.3 中不起作用

ios - Xcode 8 Frame for "Navigation Bar"will be different at run time error

macos - 在窗口调整大小时调整 Webview 的宽度/高度

python - Pygame 窗口在 Mac 上不接收键盘事件

xcode - SFML Mac 应用程序中格式错误的 Freetype 框架