Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

Convert NSData of Image from database to NSString in iPhone

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...