将图片转换成二进制写入sqlite

将图片转换成二进制写入sqlite,第1张

概述先看看网上找的例子代码: 写入: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”sqlite

先看看网上找的例子代码:

写入:

NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);Nsstring *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”sqlite.sqlite”];NSData *image=UIImagePNGRepresentation([UIImage imagenamed:@"01_02.png"]);Nsstring *aaa=[[Nsstring alloc]initWithFormat:@”update cards set c_image=? where ID=1″];sqlite3 *database;if (sqlite3_open([dataPath UTF8String],&database) == sqlITE_OK){sqlite3_stmt * complIEdStatement;sqlite3_prepare(database,[aaa UTF8String],-1,&complIEdStatement,0);sqlite3_bind_blob(complIEdStatement,1,[image bytes],[image length],NulL);int result=sqlite3_step(complIEdStatement);if (result==sqlITE_DONE) {}sqlite3_finalize(complIEdStatement);}sqlite3_close(database);[aaa release];

读取:
NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,YES);Nsstring *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”sqlite.sqlite”];sqlite3 *database;if (sqlite3_open([dataPath UTF8String],&database) == sqlITE_OK){const char *sqlStatement=”select c_image from cards where ID=1″;sqlite3_stmt *complIEdStatement;if(sqlite3_prepare(database,sqlStatement,NulL)==sqlITE_OK){while (sqlite3_step(complIEdStatement)==sqlITE_ROW) {int bytes = sqlite3_column_bytes(complIEdStatement,0);const voID *value = sqlite3_column_blob(complIEdStatement,0);if( value != NulL && bytes != 0 ){NSData *data = [NSData dataWithBytes:value length:bytes];UIImage *image=[UIImage imageWithData:data];[imageVIEw setimage:image];}else{}}}sqlite3_finalize(complIEdStatement);}sqlite3_close(database);


以上,思路就是:sqlite可以存放二进制的字段(blob),故可将图片转成NSData再写入sqlite。读取则是一个逆向过程。

在这里值得一说的是,自己尝试的时候,数据能写入,但是读出却是null。。搞了一个早上,原来是查询语句写错了。。。要细心啊

总结

以上是内存溢出为你收集整理的将图片转换成二进制写入sqlite全部内容,希望文章能够帮你解决将图片转换成二进制写入sqlite所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1173120.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存