简体   繁体   中英

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

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')

ps class for easy access to sqlite - SQLiteAccess.h - 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.

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]


Meanwhile I think you should check the field type in your SQLite DB file. Does the photo is actually set to blob .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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