nac*_*o4d 6 macos cocoa nsimage nsdocument
我正在玩AppKit和NSDocument,我不知道为什么这不起作用?:
我刚刚写了这个,图像不是零,但从不加载任何图像,它的大小始终为零.这是我必须实现的将文件读入我的文档的正确方法吗?我需要路径,而不是数据(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;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
这样做:
NSImage *image = [[NSImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
Run Code Online (Sandbox Code Playgroud)
如果从NIB文件加载imageView,则在调用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;
}
Run Code Online (Sandbox Code Playgroud)
只需确保添加NSImage*图像; 实例变量到头文件.
| 归档时间: |
|
| 查看次数: |
8328 次 |
| 最近记录: |