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

插入正常,但是当我从 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];

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

用于轻松访问 sqlite 的 ps 类 - SQLiteAccess.h - http://pastebin.com/BFxFry7T - SQLiteAccess.m - http://pastebin.com/m67yNVLm

4

1 回答 1

1

是的,我认为问题出在这里:

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

直接使用 SQLite 时,您应该通过 设置二进制数据sqlite3_bind_blob(),但实际上您将二进制数据设置为字符串。

所以当你取回数据时,它不是二进制数据,它是一个String对象,当然,一个字符串对象不响应-[NSData bytes]


同时我认为您应该检查 SQLite DB 文件中的字段类型。是否photo实际设置为blob.

于 2013-02-07T08:31:10.897 回答