简体   繁体   English

Objective-C从SQLite读取图像

[英]Objective-c read image from sqlite

    NSData *imageData = UIImageJPEGRepresentation(PhotoImage, 100);
    NSString* queryString = ; 
    [SQLiteAccess insertWithSQL:
[NSString stringWithFormat:@"insert into images (photo) values ('%@')", imageData] // photo - blob column
]; // i use class SQLiteaccess

insert is normally but when i read image from sqlite 插入通常是但当我从sqlite读取图像时

NSArray *photoSelectArray = [SQLiteAccess selectManyRowsWithSQL: [NSString stringWithFormat:@"select photo from places where id=%i", idPlace]]; 
NSDictionary *imageDataDic = [photoSelectArray objectAtIndex:0];
NSData *dataForCachedImage = [[NSData alloc] initWithData: [imageDataDic objectForKey:@"photo"]];
 UIImage *cachedImage = [UIImage imageWithData:dataForCachedImage];

i have an error - SIGABRT (Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x1e5c46d0') 我有一个错误-SIGABRT (Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x1e5c46d0')

ps class for easy access to sqlite - SQLiteAccess.h - http://pastebin.com/BFxFry7T - SQLiteAccess.m - http://pastebin.com/m67yNVLm SQLiteAccess.h - -容易获得sqlite的PS类http://pastebin.com/BFxFry7T - SQLiteAccess.m - http://pastebin.com/m67yNVLm

Yeah, I think the problem is here: 是的,我认为问题出在这里:

[NSString stringWithFormat:@"insert into images (photo) values ('%@')", imageData]]; // i use class SQLiteaccess

When using SQLite directly, you should set the binary data by sqlite3_bind_blob() , but you're actually setting the binary data as string. 直接使用SQLite时,应通过sqlite3_bind_blob()设置二进制数据,但实际上是将二进制数据设置为字符串。

So when you get back the data, it's not a binary data, it's a String object, of course, an string object don't response to -[NSData bytes] 因此,当您取回数据时,它不是二进制数据,而是String对象,当然,字符串对象不响应-[NSData bytes]


Meanwhile I think you should check the field type in your SQLite DB file. 同时,我认为您应该检查SQLite DB文件中的字段类型。 Does the photo is actually set to blob . photo实际上设置为blob吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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