来自字节数组的 NSImage

发布于 2024-11-01 16:38:15 字数 522 浏览 0 评论 0原文

我正在尝试在 NSImageView 中显示图像,图像包含在字节数组中。我该怎么做?据我了解,我需要将 byte[] 转换为 NSData 变量并将其提供给 NSImage。这是正确的吗?我该怎么做?我尝试过强制转换,但不起作用,而且似乎没有内置任何转换...

我尝试了以下操作:

强制转换:

NSData bytesAsMacVariable = (NSData) imageAsBytes;

也尝试过

NSData bytesAsMacVariable = imageAsBytes as NSData;

最后,尝试传递一个 byte[] ,就好像它是一个 NSData。

NSImage imageToShow = new NSImage(imageAsBytes);

这些都不起作用,据我所知, NSImage 或 NSData 都没有接受 byte[] 进行转换的成员函数......

I'm trying to display an image in a NSImageView, with an image contained in a Byte array. How can I do this? From what I understand I need to convert my byte[] to an NSData variable and feed that to an NSImage. Is this correct? How do I do it? I've tried casting and that doesn't work, and there doesn't seem to be any conversion built in...

I have tried the following:

Casting:

NSData bytesAsMacVariable = (NSData) imageAsBytes;

Also tried

NSData bytesAsMacVariable = imageAsBytes as NSData;

Finally, tried to pass a byte[] as if it was a NSData.

NSImage imageToShow = new NSImage(imageAsBytes);

None of these will work, and as far as I can see, neither NSImage or NSData has a member function that accepts byte[] for conversion...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

◇流星雨 2024-11-08 16:38:15

您正在转换为对象类型,但您应该转换为指向对象的指针类型。
尝试更多类似的东西

NSData *imageData = [NSData dataWithBytes:byteArray length:arrayLength];
NSImage *image = [[NSImage alloc] initWithData:imageData];
[imageView setImage:image];
[image release];

指针非常重要。

You're casting to the object type, but you should cast to pointer-to-object type.
Try something more like

NSData *imageData = [NSData dataWithBytes:byteArray length:arrayLength];
NSImage *image = [[NSImage alloc] initWithData:imageData];
[imageView setImage:image];
[image release];

The pointers are very important.

假面具 2024-11-08 16:38:15

您可以使用 NSMutableData,如下所示:

new NSImage (new NSMutableData (imageAsBytes));

You can use an NSMutableData, like this:

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