我正在使用本地主机。我通过base64encoding
方法将图像插入数据库。但我检索图像的代码不起作用。
客户端:
NSURL *url=[NSURL URLWithString:@"http://localhost/lastImage.php"];
NSHTTPURLResponse *response = nil;
NSURLRequest *req=[[NSURLRequest alloc]initWithURL:url];
NSData *urldata=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:nil];
NSDictionary *al=[NSJSONSerialization JSONObjectWithData:urldata options:0 error:nil];
NSString *name=[al objectForKey:@"name"];
NSString *imgPath=[NSString stringWithFormat:@"upload/%@",name];
NSString *escstr=[imgPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imageda=[[NSData alloc]initWithContentsOfFile:escstr];
_downlimg.image=[[UIImage alloc]initWithData:imageda];
NSLog(@"%@",name);
在提供路径时是否有任何问题。或者我应该在服务器端提供此路径
服务器端:
<?PHP
$host='localhost';
$name='root';
$pwd='';
$db='i';
$conn=mysql_connect($host,$name,$pwd);
mysql_select_db($db,$conn);
if($conn)
{
$sql=mysql_query("select name from j order by id desc limit 1");
if($sql)
{
$resultArray=array();
$tempArray=array();
while($row=mysql_fetch_assoc($sql))
{
$tempArray=$row;
array_push($resultArray,$tempArray);
}
echo json_encode($tempArray);
}
}
?>