Image Path编码为Base64 -Json文件

时间:2013-06-07 17:41:03

标签: ios json image base64

我正在构建应用程序,我使用Json文件检索菜单列表: 我在UITableview中遇到了问题,加载太慢了。

Json的例子:

"Mon": [

{
"Type" : "Breakfast",
"Name":"Sweet Potato Breakfast Taquitos",
"Price" : "OMR 1.500",
"Image":"http://img203.imageshack.us/img203/4443/satbf1.jpg",
"Description": "olive oil, sweet potato  , onions, jalapenos , cilantro , salt , pepper , shredded cheddar cheese , eggs , milk, corn tortilla , olive oil "
},

{
  "Type" : "Lunch",
"Name":"Mexican Rice",
"Price" : "OMR 1.500",
"Image":"http://img21.imageshack.us/img21/4296/mlunch1.jpg",
"Description": "rice, chicken broth, butter, olive oil, onion, garlic, tomato paste, lime juice, cilantro, cumin, salt"
}
]

我发现我必须将我的图像路径转换为Base46字符串... (我怎样才能自动执行此操作)?以及我的新路径的形式如何?

任何帮助?

1 个答案:

答案 0 :(得分:0)

1 - 如果只需要将路径转换为base64,可以使用php函数base64_encode()

$img_path = 'http://img203.imageshack.us/img203/4443/satbf1.jpg';
$b64_img_path = base64_encode($img_path);

2 - 如果您需要将图像数据转换为base64,您所要做的就是:

$img_path = 'http://img203.imageshack.us/img203/4443/satbf1.jpg';
$img_data = file_get_contents($img_path);
$b64_img = base64_encode($img_data);

3 - 使用base64_decode()解码base64字符串。

$b64_str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
$str = base64_decode($b64_str);