1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 |
CHAR *name = "test";
NSString * nameString = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
NSString * filePath = [[NSBundle mainBundle] pathForResource:nameString
ofType:@"png"]; IF ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData * imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:filePath]);
const CHAR * sequel = "insert into test_table(name,image) values(?,?)";
sqlite3_stmt * UPDATE; IF (sqlite3_prepare_v2(DATABASE, sequel, -1, &UPDATE, NULL) == SQLITE_OK) {
sqlite3_bind_text(UPDATE, 1, name, -1, NULL);
sqlite3_bind_blob(UPDATE, 2, [imgData
bytes], [imgData LENGTH], NULL); IF( sqlite3_step(UPDATE) == SQLITE_DONE) {
NSLog(@"已经写入数据"); }
sqlite3_finalize(UPDATE); } } ELSE {
NSLog(@"文件不存在"); }
下面代码从数据库中读取图片二进制数据,然后显示图片:
const CHAR * sequel = "select image from test_table where name=?";
sqlite3_stmt * getimg; IF (sqlite3_prepare_v2(DATABASE, sequel, -1, &getimg, NULL) == SQLITE_OK) { CHAR *name = "test";
sqlite3_bind_text(UPDATE, 1, name, -1, NULL); IF(sqlite3_step(getimg) == SQLITE_ROW) { INT bytes = sqlite3_column_bytes(getimg, 0);
Byte * VALUE = (Byte*)sqlite3_column_blob(getimg, 1); IF (bytes !=0 && VALUE != NULL) {
NSData * DATA = [NSData dataWithBytes:VALUE LENGTH:bytes];
UIImage * img = [UIImage imageWithData:DATA];
UIImageView * aview =[[UIImageView alloc] initWithFrame:
CGRectMake(0.0, 0.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
aview.image = img; [SELF.VIEW addSubview:aview]; [aview release]; } }
sqlite3_finalize(getimg); }
|