将图像数据转换为字符数组

时间:2021-03-06 16:34:16

I'm building a mobile advertising sdk for the iPhone and I assume that the only way to store the images that will represent the button icons for the landing page controller [in the library] is to convert the images into character arrays.

我正在为iPhone构建一个移动广告sdk,我假设,存储代表登录页面控制器(在库中)按钮图标的图像的唯一方法是将图像转换为字符数组。

When I have the character array defined inline like:

当我将字符数组定义为内联时:

const char backButtonData[] = { 0x00, 0x01, 0xFF, ... };

... I can access it like this:

…我可以这样访问它:

UIImage *backButtonImage = [UIImage imageWithData:[NSData dataWithBytesNoCopy:backButtonData length:sizeof(backButtonData) freeWhenDone:NO]];

...and set up the controller's toolbar buttons with these icon images.

…并使用这些图标图像设置控制器的工具栏按钮。

I've looked all over hoping to find a script to take image data and spit out a character array but came up empty handed so I've tried myself but have failed miserably.

我找遍了所有的地方,希望能找到一个脚本来获取图像数据并生成一个字符数组,但结果都是空的,所以我试过了,但失败得很惨。

What I need [hopefully] is a script or function to take the data from:

我需要(希望)一个脚本或函数来获取数据:

NSData *imageData = UIImagePNGRepresentation(image);

NSData * imageData = UIImagePNGRepresentation(图片);

and spit it out into this format:

然后以这种形式表达出来:

0x00, 0x01, 0xFF, ... so I can copy and past it into my source file.

0 x00 0 x01 0 xff,……这样我就可以把它复制过去,放到源文件中。

Any ideas [or link to a script or tool that will do this] ?;

有什么想法吗?

2 个解决方案

#1


4  

I think you really should look into using the UIImage class method +(UIImage*)imageNamed:(NSString *)name which will load an image file for you.

我认为你应该使用UIImage类方法+(UIImage*)imageNamed:(NSString *)的名字,它会为你加载一个图像文件。

But if you really want to embed an image file into your app as a char array have a look at the xxd command line tool and its -i option.

但是如果你真的想把一个图像文件作为一个char数组嵌入到你的应用程序中,请查看一下xxd命令行工具和它的-i选项。

#2


1  

Thanks a lot epatel, this post also saved me some struggle.

非常感谢埃帕特,这篇文章也为我省了不少力气。

xxd -i

xxd -我

really works great for creating a char array of binary data.

非常适合创建二进制数据的char数组。

#1


4  

I think you really should look into using the UIImage class method +(UIImage*)imageNamed:(NSString *)name which will load an image file for you.

我认为你应该使用UIImage类方法+(UIImage*)imageNamed:(NSString *)的名字,它会为你加载一个图像文件。

But if you really want to embed an image file into your app as a char array have a look at the xxd command line tool and its -i option.

但是如果你真的想把一个图像文件作为一个char数组嵌入到你的应用程序中,请查看一下xxd命令行工具和它的-i选项。

#2


1  

Thanks a lot epatel, this post also saved me some struggle.

非常感谢埃帕特,这篇文章也为我省了不少力气。

xxd -i

xxd -我

really works great for creating a char array of binary data.

非常适合创建二进制数据的char数组。