-2

我得到了这个 php 代码。当您访问该站点时,它会创建一个饼图:http://localhost/social/Test.php?data=100*200*100

如何从 php 文件中获取此图像到我的 Xcode 项目?当您进入站点时,图像将下载但是当您在 iPhone 上进入站点时,图像只会显示

<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...

// DON'T CHANGE ANYTHING BELOW THIS LINE...

$data = $_GET["data"];
$label = $_GET["label"];

$height = $width/2;
$data = explode('*',$data);

if ($label != '') $label = explode('*',$label);

for ($i = 0; $i < count($label); $i++) 
{
    if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
    else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
    if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}

if (is_array($label))
{
    $antal_label = count($label);
    $xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
    if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));

    $xtra_width = 5;
    if ($show_label) $xtra_width += 20;
    if ($show_percent) $xtra_width += 45;
    if ($show_text) $xtra_width += $text_length*8;
    if ($show_parts) $xtra_width += 35;
}

$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);

ImageFill($img, 0, 0, colorHex($img, $background_color));

foreach ($colors as $colorkode) 
{
    $fill_color[] = colorHex($img, $colorkode);
    $shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}

$label_place = 5;

if (is_array($label))
{
    for ($i = 0; $i < count($label); $i++) 
    {
        if ($label_form == 'round' && $show_label  && $data[$i] > 0)
        {
            imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
            imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
        }
        else if ($label_form == 'square' && $show_label && $data[$i] > 0)
        {   
            imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
            imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
        }

        if ($data[$i] > 0)
        {
            if ($show_percent) $label_output = $number[$i].' ';
            if ($show_text) $label_output = $label_output.$label[$i].' ';
            if ($show_parts) $label_output = $label_output.$data[$i];

            imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
            $label_output = '';

            $label_place = $label_place + 15;
        }
    }
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;

$data_sum = array_sum($data);

$start = 270;

for ($i = 0; $i < count($data); $i++) 
{
    $value += $data[$i];
    $end = ceil(($value/$data_sum)*360) + 270;
    $slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
    $start = $end;
    $value_counter++;
}

for ($i=$centerY+$shadow_height; $i>$centerY; $i--) 
{
    for ($j = 0; $j < count($slice); $j++)
    {
        if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
    }
}   

for ($j = 0; $j < count($slice); $j++)
{
    if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}

OutputImage($img);
ImageDestroy($img);


function colorHex($img, $HexColorString) 
{
    $R = hexdec(substr($HexColorString, 0, 2));
    $G = hexdec(substr($HexColorString, 2, 2));
    $B = hexdec(substr($HexColorString, 4, 2));
    return ImageColorAllocate($img, $R, $G, $B);
}

function colorHexshadow($img, $HexColorString, $mork) 
{
    $R = hexdec(substr($HexColorString, 0, 2));
    $G = hexdec(substr($HexColorString, 2, 2));
    $B = hexdec(substr($HexColorString, 4, 2));

    if ($mork)
    {
        ($R > 99) ? $R -= 100 : $R = 0;
        ($G > 99) ? $G -= 100 : $G = 0;
        ($B > 99) ? $B -= 100 : $B = 0;
    }
    else
    {
        ($R < 220) ? $R += 35 : $R = 255;
        ($G < 220) ? $G += 35 : $G = 255;
        ($B < 220) ? $B += 35 : $B = 255;               
    }           

    return ImageColorAllocate($img, $R, $G, $B);
}

function OutputImage($img) 
{
    header('Content-type: image/jpg');
    ImageJPEG($img,NULL,100);
}

?>
4

2 回答 2

0

检索图像数据dataWithContentsOfUrl,然后通过

  1. 用于imageWithData获取您的UIImage; 或者

  2. 将其写入NSData文件,writeToFile然后您可以稍后通过以下方式检索图像imageWithContentsOfFile

请注意,您必须将该 PHP 代码放在 iPhone 可访问的 Web 服务器上(显然localhost无法正常工作)。因此,它可能看起来像(我已将您的localhost引用替换为host.com... 您必须将其替换为您放置 PHP 代码的网站的 URL):

NSString *urlString = @"http://host.com/social/Test.php?data=100*200*100";
NSURL    *url = [NSURL URLWithString:urlString];
NSError  *err;

NSData   *imageData = [NSData dataWithContentsOfURL:url options:0 error:&err];
NSAssert(imageData, @"Unable to retrieve image");

现在您可以使用该图像:

self.imageView.image = [UIImage imageWithData:imageData];

或者,如果您愿意,现在您也可以将其写入 iPhone 上的文件:

NSString *documentsFolder  = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *documentFullPath = [documentsFolder stringByAppendingPathComponent:@"pie.jpg"]; // I noticed from the php source that this is a jpg

BOOL success = [imageData writeToFile:documentFullPath options:0 error:&err];
NSAssert(success, @"Unable to write file");

您可以稍后检索:

self.imageView.image = [UIImage imageWithContentsOfFile:documentFullPath];

请注意,这是在 iOS 中绘制饼图的一种低效方法(让 php 服务器生成然后下载的 jpg,如果您不在 wifi 上,则需要 Internet 连接并以 3G 速度下载等)。我可能会用 Core Graphics 做一些事情,或者抓住其中一种各种图表/图形 API。如果你用谷歌搜索“ ios 3d pie chart ”,你会得到很多点击。这个看起来很酷,虽然我从未尝试过。

于 2012-08-07T12:25:44.197 回答
0

请向您的应用程序发送请求表单 php 服务器,将图像转换为 base64 编码并将其作为字符串传递,然后根据需要将此文件保存为 .jpg 或 .png 等。

谢谢 djrecker

于 2012-08-07T09:49:30.993 回答