NSImage从字节数组 [英] NSImage from byte array

查看:145
本文介绍了NSImage从字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在NSImageView中显示图像,图像包含在字节数组中。我如何做到这一点?从我的理解,我需要将我的byte []转换为NSData变量,并将其馈送到NSImage。它是否正确?我该怎么做?我试过铸造,这不工作,似乎没有任何转换内置...



我试过以下: p>

投射:

  NSData bytesAsMacVariable =(NSData)imageAsBytes; 

也尝试了

  NSData bytesAsMacVariable = imageAsBytes as NSData; 

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

  NSImage imageToShow = new NSImage(imageAsBytes); 

这些都不会工作,就我所见,NSImage或NSData都没有成员函数接受byte []进行转换...

解决方案

您正在转换为对象类型, 指针到对象类型。

尝试更像

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

指针非常重要。


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...

解决方案

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.

这篇关于NSImage从字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆