Re: completely and utterly confused with NSImage + MySQL
Re: completely and utterly confused with NSImage + MySQL
- Subject: Re: completely and utterly confused with NSImage + MySQL
- From: Hayden Stainsby <email@hidden>
- Date: Sun, 26 Feb 2006 09:27:54 +0000
I'm using the MySQL-Cocoa framework (http://mysql-
cocoa.sourceforge.net/) which has a method that makes use of the
mysql_hex_string function to turn binary data into hex. After that
you need to get the binary data back again (since NSImage won't read
straight from a hex string).
Here's the code to get your image back again.
--------
int i;
const char *thumbnailHex;
thumbnailHex = [thumbnailDataHex bytes];
for (i = 0; i < strlen(thumbnailHex); i += 2) {
char aByte, tChar[2];
tChar[0] = thumbnailHex[i];
tChar[1] = thumbnailHex[i+1];
tChar[0] -= 0x30;
if (tChar[0] >= 0x0a)
tChar[0] -= 0x07;
tChar[1] -= 0x30;
if (tChar[1] >= 0x0a)
tChar[1] -= 0x07;
aByte = tChar[0] << 4;
aByte = aByte | (tChar[1]);
[thumbnailData appendBytes:&aByte length:1];
}
thumbnailImage = [[NSImage alloc] initWithData:thumbnailData];
--------
thumbnailDataHex is taken straight out of the MySQL database.
It's tested and works just fine for me. Let me know if I've missed
anything here.
--
Hayden
#!/usr/bin/perl
chop($_=<>);@s=split/ /;foreach$m(@s){if($m=='*'){$z=pop@t;$x
=pop@t;push@t,$a=eval"$x$m $z";}else{push@t,$m;}}print"$a\n";
#http://occasionallyhuman.net/
On 25 Feb 2006, at 23:57, Eric Smith wrote:
That's pretty unsatisfying... let me ask another question. Is
anyone on the list saving image data to a MySQL database? If so,
how are you doing it? Writing a hex representation of the binary
seems pretty safe.... but it doesn't work
Thanks,
Eric
On Feb 25, 2006, at 12:31 AM, Andrew Farmer wrote:
On 24 Feb 06, at 20:36, Eric Smith wrote:
If I take [tempData cString], run it through
mysql_real_escape_string and then read it back into an image,
using [[NSImage alloc] initWithData:...], the result is just a
*portion* of the original image. How much? It depends on the
level of compression. If I save an uncompressed image to the
database, it comes back out as an entire image. What is going on
here?!! Anyone have any ideas?
Two words for you: Null bytes.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40caffeineconcepts.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden