NSURL → NSImage → NSImageView

发布于 2024-10-07 11:02:37 字数 607 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2024-10-14 11:02:38

这样做:

NSImage *image = [[NSImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];

Do it this way:

NSImage *image = [[NSImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
陌上青苔 2024-10-14 11:02:38

如果 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;实例变量到你的头文件。

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.

- (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;
}

Just make sure you add a NSImage *image; instance variable to your header file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文