user1180489
user1180489

Reputation: 33

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

p.s. class for easy access to sqlite - SQLiteAccess.h - http://pastebin.com/BFxFry7T - SQLiteAccess.m - http://pastebin.com/m67yNVLm

Upvotes: 1

Views: 477

Answers (1)

Tonny Xu
Tonny Xu

Reputation: 2162

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.

Upvotes: 1

Related Questions