如何使用Objective-C从数据库中读取图像作为二进制数据?

时间:2011-11-03 16:41:30

标签: objective-c ios sqlite

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

我想将aImageUrl视为二进制文件。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

如果要存储该网址的实际图片,则需要使用BLOB(二进制大对象)。

//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]];