-1

I am converting NSData of Image from database to NSString in Iphone

Here is my code

imageData1 = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 3) length: sqlite3_column_bytes(compiledStatement, 3)];           
NSString * Str1 = [NSString base64StringFromData: imageData1 length: [imageData1 length]];
NSData *data1=[NSData base64DataFromString:Str1];
NSString* newStr1 = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];

the length of data is 6511 , but the newstr1 is nil

With reference to this question: I can't create a UTF-8 encoded string out of just any arbitrary binary data from the database - the data needs to actually be UTF-8 encoded. I have aslo done base 64 encoding and decoding. How can I solve this issue. I need it in the format of NSString only, to give it as input to a csv file?

halfer
  • 19,824
  • 17
  • 99
  • 186
Warrior
  • 39,156
  • 44
  • 139
  • 214
  • What is base64StringFromData and base64DataFromString? I can't find them in the Apple SDK. Are they functions you have written? – Nick Cartwright Mar 04 '11 at 13:58
  • i got from http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html link – Warrior Mar 04 '11 at 14:01
  • I see - thanks! When you say 'give it as input to a csv file', do you mean you would like the image data to be embedded inside a field of the CSV file itself? – Nick Cartwright Mar 04 '11 at 14:05
  • If i open the csv file in open office i should see the image on one column – Warrior Mar 04 '11 at 14:25
  • I'm not convinced this is supported in the CSV filetype. I fear you might need to actually export an Excel file to see a decoded image inside the cell.... – Nick Cartwright Mar 04 '11 at 14:39
  • How to create an Excel file in iphone? is it possible? – Warrior Mar 04 '11 at 14:41
  • Perhaps there's an easier way. Making an Excel file is non-trivial. Hopefully someone else can help out as I'm not really sure... – Nick Cartwright Mar 04 '11 at 14:56
  • Possible duplicate of [How to convert NSData of Image from database to NSString in iPhone?](https://stackoverflow.com/questions/5183100/how-to-convert-nsdata-of-image-from-database-to-nsstring-in-iphone) – halfer Jan 19 '18 at 16:11
  • (This seems to be a duplicate of your prior question a day earlier, same to the degree that the titles were identical). – halfer Jan 19 '18 at 16:12

1 Answers1

0

I don't have ample knowledge of character encoding, but would it not be sufficient to just use the Base 64 encoded string (Str1) in your output?

I fear that re-encoding Str1 as data1 brings you right back to square 1 and undoes any Base 64 encoding done in the line earlier.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nick Cartwright
  • 8,334
  • 15
  • 45
  • 56