Pass MySQL medium blob into IOS NSData\UIImage

青春壹個敷衍的年華 提交于 2019-12-25 02:08:27

问题


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];

来源:https://stackoverflow.com/questions/16010499/pass-mysql-medium-blob-into-ios-nsdata-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!