Objective c NSTask在xCode中启动问题
我正试图使用此代码在应用程序包中获取ImageMagick库,但这非常复杂:Objective c NSTask在xCode中启动问题,objective-c,xcode,macos,cocoa,Objective C,Xcode,Macos,Cocoa,我正试图使用此代码在应用程序包中获取ImageMagick库,但这非常复杂: -(id)init { if ([super init]) { NSString* bundlePath = [[NSBundle mainBundle] bundlePath]; NSString* imageMagickPath = [bundlePath stringByAppendingPathComponent:@"/Contents/Resource
-(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\u LIBRARY\u路径
和MAGICK\u 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];
}
和当前错误(在控制台中):
必须使用可执行二进制文件的路径调用[task setLaunchPath:…]
。在“更新2”代码中,您使用目录路径调用它 NSString*MAGICK\u HOME,*DYLD\u LIBRARY\u PATH
@Julius:你在哪里声明MAGICK\u HOME,DYLD\u LIBRARY\u PATH?请显示您的更新代码。检查pwd
是否真的包含预期的路径,无论是在调试器中还是在NSLog(@“pwd=%@”,pwd)
之前[任务启动]
。好的,我只需要一点时间来下载新的xCode,因为山狮更新了操作系统,我现在不能使用旧的xCode,所以我无法打开我的项目。用当前代码更新了问题,并在更改目录位置后遇到另一个错误
*** 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.