-2

EDIT reformulated question:

i am loading an image from a server with my iphone the following way.

NSURL *url = [NSURL urlWithString:@"http://www.example.de/getImage.php"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *im = [UIImage imageWithData:data];
imageView.image = im;

the php script ends with the line

$image = file_get_contents($direction);
echo $image;

everything is working here. i reveice the image. Now i wanted to receive two objects of the server and tried to change the script slightly:

echo array(0=>$image , 1=> $intValue);

i tried to read this data for hours now and don´t get a result. why doesn´t this try work?:

NSArray *arr = [[NSArray alloc] initWithContentsOfURL:url];
UIImage *im = [UIImage imageWithData:[arr objectAtIndex:0]];

while debugging i used NSLog to get the description of the image and it is (null).

There is no typo or compiling or syntax error...

EDIT001: Okay i got it now perhaps a little overkill but the way i tried is not possible in my opinion. i used json_encode and base64_encode again like this:

echo json_encode(array('a'=>base64_encode($image), 'b'=>$anotherObject));

on iphone side i used some frameworks to decode JSON and base64 again and it works... thank you and if you have questions on this write them down

  • perhaps a typo, but `UIImage *im = [[UIImage imageWithData:[arr objectAtIndex:0];` has three `[` but only one `]` – horatio Jun 01 '11 at 15:05
  • sry... entered to fast. everthing compiles perfectly and it runs... but the image can´t be read... – Maxi Buschmann Jun 01 '11 at 15:11
  • file_get_contents('$direction'); cannot be right. Should either be ("$direction") or just ($direction). – Flipper Jun 01 '11 at 15:12
  • ok... sorry about my typo-faults... but there must be a more deep error. it works when i echo only the image and it doesn´t give any errors or warnings or syntax problems. just the image seems to be a pointer to nil when using the array stuff... – Maxi Buschmann Jun 01 '11 at 15:15

2 Answers2

0

From the PHP manual page for 'json_encode':

Can be any type except a resource. This function only works with UTF-8 encoded data

Perhaps send the image as a separate http get request so that you don't need to worry about encoding / json parsing of the image data.

Matt Connolly
  • 9,757
  • 2
  • 65
  • 61
  • i edited the question now after doing it without json_encode...thank you for your hint! – Maxi Buschmann Jun 01 '11 at 15:01
  • Might one base64-encode the image data so it can be used with json_encode? – horatio Jun 01 '11 at 15:34
  • Yeah, I thought of the base64 thing too, but there's no built in base64 decoding function in iOS that I'm aware of. Not that it's complex, and there's plenty of sample code for base64 decoding around. It's probably just as easy to get the NSData from a url request, and load the image from that, unless you really need to put it all into one request and json response. – Matt Connolly Jun 01 '11 at 23:28
0

$image = file_get_contents('$direction'); echo $image;

If this is your literal PHP code, you should get an error: '$direction' is the literal string '$direction'. I can not imagine you actually have a file that's called $direction. There shouldn't be a need to place the variable in quotes:

echo file_get_contents( $direction );
Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
  • yes already got my typo fault... SORRY guys!!... but then it wouldn´t have worked with the echo $image. the point is: the first scheme works when echoing the image and the socond doesn´t when echoing a php array! there must be an error – Maxi Buschmann Jun 01 '11 at 15:18