我在玩AppKit和NSDocument,我不知道为什么这不起作用?:
我刚写了这段代码,image不是nil,但从不加载任何图像,它的大小始终为零。这是我必须实现的将文件读入我的文档的正确方法吗?我需要路径,而不是数据(NSData),因为我计划使用其他库来读取其他文件。
现在我正在尝试阅读PNG,JPG,都不起作用。;(
- (BOOL) readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError{
NSImage *image = nil;
image = [[NSImage alloc] initWithContentsOfURL:url];
[imageView setImage:image];
[image release];
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return YES;
}提前谢谢。
发布于 2010-12-16 05:23:44
如果imageView是从NIB文件加载的,则在调用readFromURL:ofType:error:时不会对其进行设置。相反,您应该加载图像并将其存储在实例变量中,然后将其添加到windowControllerDidLoadNib:方法中的imageView中。此外,您每次都会返回一个错误,而您应该只在出现错误时才返回一个错误。
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController {
[imageView setImage:image];
[image release];
image = nil;
}
- (BOOL)readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError {
image = [[NSImage alloc] initWithContentsOfURL:url];
if(!image) {
if(outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
return NO;
}
return YES;
}只需确保将NSImage *image;实例变量添加到头文件中即可。
发布于 2010-12-16 03:05:33
这样做:
NSImage *image = [[NSImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];https://stackoverflow.com/questions/4453725
复制相似问题