views:

526

answers:

2

I'm trying to serve up png images from a Linux (c++ / Qt4.5.x) server daemon to an iPhone application that is using the Three20 framework - specifically I want to use the TTThumbsViewController view.

I managed to make any web browser view images with the following returned in my daemon when it "GET"s a request:

QTextStream os(socket);
os.setAutoDetectUnicode(true);
QByteArray base64 = array.toBase64();

os << "HTTP/1.1 200 Ok\r\n"
  "Host: software.local\r\n"
  "\r\n"
  "<html>"
  "<body>"
  "<img src=\"data:image/png;base64," << base64 << "\" />"
  "</body>";

where "array" is a png's image data; I'm typing something like:

  http://software.local:8080/test.png

in to the browser to view the image.

When I try and specify the same URL in my photo source class with something like

[MockPhoto alloc]
 initWithURL:@"http://software.local:8080/test.png"
 smallURL:@"http://software.local:8080/test.png"
 size:CGSizeMake(480, 320)] autorelease],
...

nothing is returned or displayed?

My question is really - if I put say test.png in a suitable directory on the Linux PC and start a web server (apache), then browse to "http://software.local/test.png I see the image as above, but the image was not embedded in the http header? I really can't figure out what the header should be to get this behaviour. If I set the URL in the above iPhone code so it loads the png from the apache server I see it in the TTThumbsViewController.

Any help would great, or better way to do this - I only have basic http experience, as you can see.

+1  A: 

Hi,

Your script isn't serving an image, it's serving html that will be interpreted by a browser. I've never used three20's framework but I bet they're expecting a png to returned as data, nt embedded into a html document. As the browser you are testing with understands html as well as raw image data, it will display the image fine.

To get this to work you will need to set the content-type header to 'image/png' and then send the image data. I don't know how to do this in QT though, sorry :(

Sam

deanWombourne
Thanks for the reply. I can see there are 2 ways to 'return' an image, but when I made my server return this: os << "HTTP/1.1 200 Ok\r\n" "Location: http://software.local:8080/test.png\r\n" "Content-Type: image/png\r\n" "Content-Length: " << base64.size() << "\r\n" "Content-transfer-encoding: base64\r\n" "\r\n" << base64;I can an error saying can't load image. I failed to find any info on the web regarding how to return the image raw.
petert
Hmmm, that's odd. Have you tried it without the Location header in your response? Apart from that, I don't know enough about qt / web to help anymore, sorry!
deanWombourne
Still no go - what I'm trying to figure out is how and where to add the image raw or base64 data?? Is it in the header itself or in the body raw? I did try putting the image data in the 'body', but no joy. No idea how to put image in header, if possible? Does any web guru know where I'm going wrong? Thanks.
petert
A: 

I used to do this with PHP. Translate these headers into your QT echo string.

header('Last-Modified: ' . date('r'));
header('Accept-Ranges: bytes');
header('Content-Length: ' . $imageSize);
header('Content-Type: image/png');
print(file_get_contents($file));

Not too sure about base64 if it's necessary or not

scootklein
Thanks for hinting at something to try - I'll report back any findings.
petert