NSURL → NSImage → NSImageView
我正在使用 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;
}
提前致谢。
I am playing with AppKit and NSDocument and I don't know why this is not working?:
I just wrote this and image is not nil but never loads any image, its size is always zero.
Is this the correct method I have to implement to read files into my document?
I need the path, not the data (NSData) because I plan to use other library to read other files.
Now I am trying to read PNGs, JPGs , none worked. ;(
- (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;
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做:
Do it this way:
如果 imageView 是从 NIB 文件加载的,则在调用 readFromURL:ofType:error: 时不会设置它。相反,您应该加载图像并将其存储在实例变量中,然后将其添加到 windowControllerDidLoadNib: 方法中的 imageView 中。此外,您每次都会返回一个错误,而只有在出现问题时才应该返回错误。
只需确保添加 NSImage *image;实例变量到你的头文件。
If imageView is being loaded from a NIB file, it will not have been set when readFromURL:ofType:error: is called. Instead, you should load the image and store it in an instance variable, then add it to the imageView in the windowControllerDidLoadNib: method. Also, you are returning an error every time, while you should only return an error if something goes wrong.
Just make sure you add a NSImage *image; instance variable to your header file.