0

In my PHP, I'm tying to send an array with data from the database. All values should be accepted as NSString exept one image that is stored as 'medium blob' in the database, and I'm trying to pass it's NSData\UIImage value.

while ($row = mysql_fetch_array($result)) {

    $add = array(); 
    $add["Id"] = $row["Id"]; 
    $add["Mail"] = $row["Mail"];
    $add["Category"] = $row["Category"]; 
    $add["Phone"] = $row["Phone"]; 
    $add["Msgs"] = $row["Msgs"];
    //image from blob
    $add["Picture"] = $row["Picture"];

    // push single add into final response array 
    array_push($response["adds"], $add); 
} 
// success 
$response["success"] = 1; 
$response["message"] = "Adds loaded successfully";
// echoing JSON response 
echo json_encode($response); 

Now in Xcode, all values are received just fine except the image that is NULL. I tried to encode it with base64 in PHP and decode in Objective-C, but xcode collapses because it's still NULL. I tried to define:

header("Content-type: image/png");

or

file_put_contents("image.png", $add["Picture"]);

but it's always received as NULL. Rows that don't have blobs stored in them return as ' ', only the one with content returns NULL. I'm new to PHP, and I'm sure I'm doing some basic stupid mistake.

BTW my Objective-C code is:

const void *bytes = [[addPicture objectAtIndex:indexPath.row] bytes];
int length = [[addPicture objectAtIndex:indexPath.row] length];
NSData* imageData = [[NSData alloc] initWithBytes:bytes length:length];
aCell.cellBackImg.image = [UIImage imageWithData:imageData];
Expedito
  • 7,771
  • 5
  • 30
  • 43
ItayAmza
  • 819
  • 9
  • 21
  • I normally use the base64_encode($row["Picture"]) to save the data in the database and the base64_decode($row["Picture"]) to get the image from the select result. Try the decode instead the encode to get it. – costa Apr 15 '13 at 08:33
  • files base64 encoded is 33% bigger so I decode them before inserting into DB, so when I SELECT the image from DB its not encoded so there is no need to decode – ItayAmza Apr 15 '13 at 08:39

0 Answers0