Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以将图像作为字符串存储在cocoa中?_Cocoa_Image - Fatal编程技术网

是否可以将图像作为字符串存储在cocoa中?

是否可以将图像作为字符串存储在cocoa中?,cocoa,image,Cocoa,Image,我想硬编码类文件中图像的二进制数据。初始化该类时,从该数据创建NSImage。将图像存储在resources文件夹中不是一个选项 这是否可能以及如何实现?使用NSData而不是NSString NSImage符合NSCoding的要求-它知道如何存档自身,以及如何创建/读取其他文件格式的图像表示 如果要使用其他图像表示,可以使用CGImageapi创建CGImage,然后可以使用它创建NSImage //get the image NSImage *newImage = [[NSImage al

我想硬编码类文件中图像的二进制数据。初始化该类时,从该数据创建NSImage。将图像存储在resources文件夹中不是一个选项


这是否可能以及如何实现?

使用
NSData
而不是
NSString

NSImage
符合
NSCoding
的要求-它知道如何存档自身,以及如何创建/读取其他文件格式的图像表示

如果要使用其他图像表示,可以使用
CGImage
api创建
CGImage
,然后可以使用它创建
NSImage

//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];
我在这里使用了两个NSData类别(EncodeBase64 with newlines:NO和DecodeBase64 with newlines:NO),您必须包含libcrypto.dylib才能使它们工作。我想我是从你那里抄来的


也许你应该看看base64编码?看,我可以把它放到NSData中没有问题,但是在那之后我该怎么处理它呢?至于NSCoding,会保存到plist还是我可以复制粘贴的东西?对于c源,从NSData表示到数据blob的短路径是创建一个简单的程序,重新格式化
[data description]
返回的数据表示的输出,然后将其打印到控制台,并将其复制/粘贴到源文件(或让其生成源文件)。我认为这是可行的。“创建一个简单的程序,重新格式化数据表示的输出”,这是一个大问题。它应该如何格式化?或者,我可以从[data description]复制打印输出并将其粘贴到源代码中,然后调用NSData*fromDataDescription=[NSData dataFromString]?描述的结果是实现定义的。从我看到的转储方式来看,基本需求是将其转换为无符号32位十六进制值(prepend
0xU
),并为结果数组中的每个值添加32位主机->大端交换。
- (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;
}