iOSsqlite存儲(chǔ)圖片內(nèi)容并展示

首先我們要在.h中導(dǎo)入頭文件

#import <sqlite3.h>

#import "Model.h"

當(dāng)然不要忘記導(dǎo)入sqlite的庫(kù)

然后實(shí)現(xiàn)一些方法

{

//給數(shù)據(jù)庫(kù)創(chuàng)建一個(gè)指針;

sqlite3 * sqliteDB;

NSString *newpath;

}

//單例

+(instancetype)shareData;

//初始化數(shù)據(jù)庫(kù)

-(void)create;

//添加數(shù)據(jù)

-(void)insert:(Model *)mo;

//查詢數(shù)據(jù)

-(NSMutableArray *)select;

////關(guān)閉數(shù)據(jù)庫(kù)

//-(void)open;

//關(guān)閉數(shù)據(jù)庫(kù)

-(void)close;

@end

其次實(shí)在.m中實(shí)現(xiàn)這些方法

static LoadData * ld = nil;

@implementation LoadData

//單例

+(instancetype)shareData{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

ld = [[LoadData alloc]init];

});

return ld;

}

+(instancetype)allocWithZone:(struct _NSZone *)zone{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

ld = [super allocWithZone:zone];

});

return ld;

}

-(id)copy{

return self;

}

-(id)mutableCopy{

return self;

}

//初始化數(shù)據(jù)庫(kù)

-(void)create{

//創(chuàng)建沙盒路徑

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

//創(chuàng)建沙盒路徑下的.db文件

newpath = [path stringByAppendingPathComponent:@"by.db"];

//打開數(shù)據(jù)庫(kù)

sqlite3_open([newpath UTF8String], &sqliteDB);

//添加表格

[self inittable];

}

//創(chuàng)建表格

-(void)inittable{

//創(chuàng)建sql語句

const char * sql = "create table imgtable ( id integer primary key, image text)";

//創(chuàng)建預(yù)編譯指針

sqlite3_stmt * stmt;

//把sql語句綁定到預(yù)編譯指針中

sqlite3_prepare(sqliteDB, sql, -1, &stmt, nil);

//執(zhí)行

sqlite3_step(stmt);

//釋放預(yù)編譯指針

sqlite3_finalize(stmt);

}

//添加數(shù)據(jù)

-(void)insert:(Model *)mo{

//? ? const char * sqla = "insert into aiya values(null,?)";

//創(chuàng)建sql語句

const char * sql = "insert into imgtable values (null,?)";

sqlite3_stmt * stmt;

sqlite3_prepare(sqliteDB, sql, -1, &stmt, nil);

sqlite3_bind_text(stmt, 1, [mo.img UTF8String], -1, nil);

//綁定問號(hào)

//? ? sqlite3_step(stmt);

if (sqlite3_step(stmt) == SQLITE_DONE) {

NSLog(@"成功");

}

sqlite3_finalize(stmt);

}

//查詢數(shù)據(jù)

-(NSMutableArray *)select{

const char * sql = "select * from imgtable";

sqlite3_stmt * stmt;

sqlite3_prepare(sqliteDB, sql, -1, &stmt, nil);

NSMutableArray * arr = [[NSMutableArray alloc]init];

while (sqlite3_step(stmt) == SQLITE_ROW) {

Model * M = [[Model alloc]init];

M.img = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];

NSData * data = [[NSData alloc]initWithBase64Encoding:M.img];

[arr addObject:data];

}

return arr;

}

//關(guān)閉數(shù)據(jù)庫(kù)

-(void)close{

sqlite3_close(sqliteDB);

}

然后在viewcontroller中設(shè)置兩個(gè)按鈕并實(shí)現(xiàn)其方法

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"name" style:UIBarButtonItemStyleDone target:self action:@selector(back)];;

-(void)back{

Model * m = [[Model alloc]init];

NSData * data = UIImageJPEGRepresentation(self.img.image, 0.5f);

m.img = [data base64Encoding];

[[LoadData shareData]insert:m];

MyViewController * my = [[MyViewController alloc]init];

[self.navigationController pushViewController:my animated:YES];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容