简体   繁体   中英

How can I read image as binary data from database using Objective-C?

NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

I want to read aImageUrl as binary. How can I do that?

You need to use a BLOB (Binary Large OBject) if you want to store the actual image from that URL.

//Storage
UIImage *image = ...; //Retrieve image from URL
NSData *imageData = UIImagePNGRepresentation(image);
sqlite3_bind_blob(compiledStatement, 1, [imageData bytes], [imageData length], NULL);

//Retrieval
const void *data = sqlite3_column_blob(compiledStatement, 1);
int length = sqlite3_column_bytes(compiledStatement, 1);
UIImage *retrieved = [UIImage imageWithData:
                          [NSData dataWithBytes:data length:length]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM