【发布时间】:2011-05-28 18:26:34
【问题描述】:
我想在类文件中硬编码图像的二进制数据。当类被初始化时,从该数据创建一个 NSImage 。不能将图像存储在资源文件夹中。
这可能吗?如何实现?
【问题讨论】:
-
也许你应该看看base64编码?
我想在类文件中硬编码图像的二进制数据。当类被初始化时,从该数据创建一个 NSImage 。不能将图像存储在资源文件夹中。
这可能吗?如何实现?
【问题讨论】:
使用NSData 而不是NSString。
NSImage 与 NSCoding 兼容 - 它知道如何存档自己,以及如何创建/读取其他文件格式的图像表示。
如果您想使用其他图像表示,可以使用CGImage api 创建一个CGImage,然后可以使用它创建一个NSImage。
【讨论】:
[data description] 返回的数据表示的输出,然后将其打印到控制台并复制/将其粘贴到您的源文件(或让它生成源文件)。那,我想,会工作的。
description 的结果是实现定义的。从我看到的转储方式来看,基本需求是将其转换为无符号的 32 位十六进制值(前置 0x<NUMBER>U)并为结果数组中的每个值添加 32 位主机->大端交换。
//get the image
NSImage *newImage = [[NSImage alloc] initWithContentsOfFile:@"~/Desktop/testImg.png"];
//convert to BitmapImageRep
NSBitmapImageRep *bitmap = [[newImage representations] objectAtIndex:0];
//convert to NSData
NSData *data = [bitmap representationUsingType: NSPNGFileType properties: nil];
//base64 encode and now I have the string.
NSString *imageString = [data encodeBase64WithNewlines:NO];
NSLog(@"image %@", imageString);
//No that I have the string, I can hard code it into my source code (paste it in).
//When I want to create an image out of it I just get the imageString and convert it to an image
NSData *revData = [imageString decodeBase64WithNewlines:NO];
newImage = [[NSImage alloc] initWithData:revData];
我在这里使用了 2 个 NSData 类别(encodeBase64WithNewlines:NO 和 decodeBase64WithNewlines:NO)您必须包含 libcrypto.dylib 才能使它们工作。我想我是从Cocoa Dev复制它们的
- (NSString *) encodeBase64WithNewlines: (BOOL) encodeWithNewlines
{
// Create a memory buffer which will contain the Base64 encoded string
BIO * mem = BIO_new(BIO_s_mem());
// Push on a Base64 filter so that writing to the buffer encodes the data
BIO * b64 = BIO_new(BIO_f_base64());
if (!encodeWithNewlines)
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
mem = BIO_push(b64, mem);
// Encode all the data
BIO_write(mem, [self bytes], [self length]);
int flushResult = BIO_flush(mem);
if(flushResult != 0){
//throw some warning?
}
// Create a new string from the data in the memory buffer
char * base64Pointer;
long base64Length = BIO_get_mem_data(mem, &base64Pointer);
NSData * base64data = [NSData dataWithBytesNoCopy:base64Pointer length:base64Length freeWhenDone:NO];
NSString * base64String = [[NSString alloc] initWithData:base64data encoding:NSUTF8StringEncoding];
// Clean up and go home
BIO_free_all(mem);
return [base64String autorelease];
}
- (NSData *)decodeBase64WithNewLines:(BOOL)encodedWithNewlines
{
// Create a memory buffer containing Base64 encoded string data
BIO * mem = BIO_new_mem_buf((void *) [self bytes], [self length]);
// Push a Base64 filter so that reading from the buffer decodes it
BIO * b64 = BIO_new(BIO_f_base64());
if (!encodedWithNewlines)
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
mem = BIO_push(b64, mem);
// Decode into an NSMutableData
NSMutableData * data = [NSMutableData data];
char inbuf[512];
int inlen;
while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0)
[data appendBytes: inbuf length: inlen];
// Clean up and go home
BIO_free_all(mem);
return data;
}
【讨论】: