2012-07-26 4 views
1

I 앱 번들 내 ImageMagick이 라이브러리를 얻기 위해이 코드를 사용하기 위해 노력하고있어,하지만 매우 복잡 :NSTask 출시

-(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. 
+1

NSString * MAGICK_HOME, * DYLD_LIBRARY_PATH' – mask8

+0

@Julius : MAGICK_HOME, DYLD_LIBRARY_PATH은 어디에 선언합니까? 업데이트 된 코드를 보여주십시오. 디버거 나'[task launch]'바로 전에'NSLog (@ "pwd = % @", pwd)'와 함께'pwd'가 실제로 예상 경로를 포함하고 있는지 확인하십시오. –

+0

업데이트 된 OS가 산 사자에게나 오래된 xCode를 사용할 수 없기 때문에 새로운 xCode를 다운로드하기 만하면됩니다. 따라서 프로젝트를 열 수 없습니다. – hockeyman

답변

2

[task setLaunchPath:...]은 실행 가능한 바이너리에 대한 경로로 호출해야합니다. "UPDATE 2"코드에서 디렉토리 경로로 호출합니다.