2012-07-26 37 views
1

我想使用此代碼得到應用捆我ImageMagick庫,但它是非常複雜的:NSTask推出

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

標識符(解決)

更新:

但是,當我嘗試構建並運行它時,我的應用程序崩潰。崩潰在:[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. 
+1

'NSString * MAGICK_HOME,* DYLD_LIBRARY_PATH' – mask8 2012-07-26 06:43:40

+0

@Julius:你在哪裏聲明MAGICK_HOME,DYLD_LIBRARY_PATH?請顯示您的更新代碼。檢查'pwd'是否真的包含了期望的路徑,無論是在調試器中還是在'[task launch]之前'使用'NSLog(@「pwd =%@」,pwd)''。 – 2012-07-26 11:10:45

+0

好吧,只需要一點點下載新的xCode,因爲升級到山獅的操作系統,我現在不能使用舊的xCode,所以我無法打開我的項目。 – hockeyman 2012-07-26 11:18:21

回答

2

[task setLaunchPath:...]必須與路徑的可執行二進制被調用。在你的「UPDATE 2」代碼中,你可以用一個目錄路徑來調用它。