CakePHP的:让图像从MySQL BLOB到iOS
问题描述:
我试图使用下面的代码CakePHP的:让图像从MySQL BLOB到iOS
$this->autoRender=false;
$blob = $this->GeneralNews->findById($id,array("image_data"));
$image = imagecreatefromstring($blob["GeneralNews"]["image_data"]);
ob_start(); //You could also just output the $image via header() and bypass this buffer capture.
imagejpeg($image, null, 80);
$data = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/jpg;base64,' . base64_encode($blob["GeneralNews"]["image_data"]) . '" />';
结果可能在未来的URL被视为从数据库中获得的图像 http://www.thedarkdimension.com/generalNews/displayImage/1678
但是当我尝试在
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
,我得到的是空虽然在(为imageData)
数据的图像通过的iOS得到这个我不知道这个问题,但它最有可能来自PHP方面..因为我试过这个URL http://www.johnquarto.com/wp-content/uploads/2008/08/gag.jpg 它可以与它。
答
已经很快看了看起来,从URL http://www.thedarkdimension.com/generalNews/displayImage/1678返回的内容不是图像而是声明为“text/html”类型的文档。该文档实际上并不是有效的HTML,但只包含一个单一的img标记,其source属性是base64编码的图像。鉴于您的PHP代码,这是有道理的。
我怀疑imageWithData正在寻找什么,但只是它的所有二进制良好的JPEG。
因此,您需要更改您的PHP以将MIME类型设置为image/jpeg,然后仅按照原样回显jpeg数据 - 不需要base64编码。我没有看过PHP这么久,我不敢给你示例代码,但希望这足以让你去。
这似乎是答案的一部分。为了帮助OP实现这一点,请查看CakePHP'Response'对象,了解如何使用CakePHP提供原始图像数据; http://book.cakephp.org/2.0/en/controllers/request-response.html#sending-files随时在您的回答中包含此内容 – thaJeztah 2013-03-16 13:36:44