Convert BLOB image from NSData to NSString so it c

2019-09-17 15:21发布

问题:

So I successfully retrieved my BLOB image from the database and set it to NSData *content..

content = [[NSData alloc]
       initWithBytes:sqlite3_column_blob(query, 16)
       length:sqlite3_column_bytes(query, 16)];

What I am trying to do is to get this content into a NSString, because I would like to then pass this string to javascript and display the image. I am not exactly sure if this is even possible, but i have read on the link below that you can display images in css/html in base64. http://mark.koli.ch/2009/07/howto-include-binary-image-data-in-cascading-style-sheets-css.html

The issue I am having is converting my NSData to an NSString so I can then put it into my UIWebView's loadHTMLString....I cannot use a UIImage, must be a UIWebView.

Thanks! Greg

回答1:

content = [[NSData alloc] initWithBytes:sqlite3_column_blob(query, 16) length:sqlite3_column_bytes(query, 16)]; NSString *path = [webDirectory stringByAppendingPathComponent:@"image.png"]; [content writeToFile:path atomically:NO]; [content release]; 

I was able to figure out my solution by writing the file out to a png. Where webDirectory is your directory of choice