How to serialize NSImage to an sql script in order to import it in a blob column?
My iphone app uses a SQLite database to store data. I have a table with a blob column where i store my images.
When i do an update i don't want to overwrite the user's database, i want to开发者_如何学运维 execute some sql scripts and inject new data if needed.
I have a utility app made for Mac that should make the sql scripts that will be run on the iphone.
I have the images stored as NSImages in my app but i have problems when i want to export the data as sql scripts(simple text files).
My files should have lines like:
Insert into Images(imageData) values ( ___IMAGE1_DATA___ );
Insert into Images(imageData) values ( ___IMAGE2_DATA___ );
Insert into Images(imageData) values ( ___IMAGE3_DATA___ );
the question is how could i serialize images data to my sql script in order to import the data correctly into the blob column?
You can use TIFFRepresentation of the NSImage to get hold of the NSData representation.
Actually I would save the images to disk and reference those only from your sqlite database. This can improve performance when your images tend to be large.
You will need to get the underlying CGImage
object, using the CGImage
method of UIImage. Then take a look at the CGImage reference:
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGImage/Reference/reference.html
Then use a CGContextRef
object to draw the image, and obtain the pixel data.
There's a good example on StackOverflow:
How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?
You can also use the UIImagePNGRepresentation
or UIImagePNGRepresentation
functions, that return directly a NSData
object, if those formats suits you. It will be a lot simpler.
Then use the bytes
method of NSData
to have a pointer to the image data (const void *
).
I found a solution to serialize the images to an sql script and then insert them into the db (blob column).
I extract the NSData from an image like this NSData *thumbailData = [thumbnail TIFFRepresentation];
(thanks Nick).
After i extract the NSData, i convert it into a hex string using the method below. I added it to a category of NSData.
- (NSString*) hexString {
NSMutableString *stringBuffer = [NSMutableString
stringWithCapacity:([self length] * 2)];
const unsigned char *dataBuffer = [self bytes];
int i;
for (i = 0; i < [self length]; ++i)
[stringBuffer appendFormat:@"%02x", (unsigned long)dataBuffer[ i ]];
return [[stringBuffer copy] autorelease];
}
NSString *hexRepresentation = [thumbnailData hexString];
The hexRepresentation will look like below:
4d4d002a00005a48fafafafff8f8f8fff8f8f8fff9f9f9fff8f8f8fff8f8f8 …
In order to serialize the hexRepresentation of the image i created an SQL script like below:
INSERT INTO Thumbnails (Picture_uid, Thumbnail) Values(10, x'4d4d002a00005a48fafafafff8f8f8fff8f8f8fff9f9f9fff8f8f8fff8f8f8 … ‘) ;
the x' data ' tells the db that it will receive info in hex format and it will know how to deal with it.
one of the problems with this solution is that it will double the size of the script. if you'll have an image of 200kb the script will have 400kb but in the db the image will be 200kb.
for me this was a good solution to update my db using sql scripts without writing any code.
精彩评论