2011-05-28 64 views
2

我想在類文件中硬編碼圖像的二進制數據。當類被初始化時,從該數據創建一個NSImage。將圖像存儲在資源文件夾中不是一個選項。是否可以將圖像作爲字符串存儲在可可中?

這是可能的和如何?

+0

也許你應該看看base64編碼? – galymzhan 2011-05-28 18:28:23

+0

看到http://stackoverflow.com/questions/225480/embed-image-in-code-without-using-resource-section-or-external-images/225658#225658 – 2011-05-28 19:00:58

回答

3
//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; 
} 
4

使用NSData而不是NSString

NSImageNSCoding兼容 - 它知道如何存檔自己,以及如何創建/讀取其他文件格式的圖像表示。

如果您想使用其他圖像表示法,則可以使用CGImage apis創建一個CGImage,然後可以使用它來創建NSImage

+0

我可以把它納入NSData沒有問題,但什麼那我以後用它做?至於NSCoding,將它保存到plist還是我可以複製和粘貼的東西? – joels 2011-05-28 22:56:52

+0

從NSData表示到數據blob爲ac源的短路徑是創建一個簡單的程序,它重新格式化[數據描述]返回的數據表示的輸出,然後將其打印到控制檯並將其複製/粘貼到您的源文件(或者讓它生成一個源文件)。我認爲,這會起作用。 – justin 2011-05-29 00:28:13

+0

「創建一個簡單的程序,重新格式化數據表示的輸出」這是一個大問題。它應該如何格式化?或者,我可以從[數據描述]中複製打印出來並粘貼到源代碼中var NSString * dataDescription =「printOutFromNSLog」,然後調用NSData * fromDataDescription = [NSData dataFromString]? – joels 2011-05-29 02:58:06

相關問題