1

我正在将图像从 iPhone 上传到 PHP,但在 PHP 中图像不可见。图像以字节为单位保存。

  -(void)uploadMyImage:(UIImage *)img
    {
        NSData *data = UIImageJPEGRepresentation(img, 90);
        NSMutableString *urlString = [[NSMutableString alloc] init];
        [Base64 initialize];
        [urlString appendFormat:@"image=%@",[Base64 encode:data]];
          NSString *baseurl = @"http://projectleads.info/spirits/images/imageupload1.php"  ;
        NSData *postData = [urlString dataUsingEncoding:NSUTF8StringEncoding];
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
        NSURL *url = [NSURL URLWithString:baseurl];
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
        [urlRequest setHTTPMethod: @"POST"];
        [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [urlRequest setValue:@"application/x-www-form-urlencoded"
          forHTTPHeaderField:@"Content-Type"];
        [urlRequest setHTTPBody:postData];
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
        [connection start];
        }

  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        resData=[[NSMutableData alloc]init];
    }
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [resData appendData:data];
    }
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSString* newStr = [[NSString alloc] initWithData:resData
                                                 encoding:NSUTF8StringEncoding];
    }

我的php代码是

<?php
    $base=$_REQUEST['image'];
     if(empty($base))
    {
        echo 'No data';
    }
    else
    {
    echo $base;
    $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');
        // print($binary);
    //$theFile = base64_decode($image_data);
    $dbser='localhost';  
    $dbuser='spirit';
    $dbpwd='123456';
    $dbname='spirit';
       $db= mysql_connect($dbser,$dbuser,$dbpwd);
     $dbcon=mysql_select_db($dbname);
     function generate_chars()
    {
        $num_chars = 4; //max length of random chars
        $i = 0;
        $my_keys = "123456789abcdefghijklmnopqrstuvwxyz"; //keys to be chosen from
        $keys_length = strlen($my_keys);
        $url  = "";
        while($i<$num_chars)
        {
            $rand_num = mt_rand(1, $keys_length-1);
            $url .= $my_keys[$rand_num];
            $i++;
        }
        return $url;
    }

     function isUnique($chars)
    {
        //check the uniqueness of the chars
        global $link;
        $q = "SELECT * FROM `url` WHERE `randname`='".$chars."'";
        $r = mysql_query($q, $link);
        //echo mysql_num_rows($r); die();
        if( mysql_num_rows($r)>0 ): 
            return false;
        else: 
            return true;
        endif;
    }

     $chars = generate_chars();
     while( !isUnique($chars) )
     {
            $chars = generate_chars();
     }
    $query=mysql_query("insert into url (randname) values ('".$chars."')");
    $test=$chars.".jpg";
    $file = fopen($test, 'wb');
    fwrite($file, $binary);
    fclose($file);
    echo 'success';
    echo '<img src=test.jpg>';
    }

?>
4

2 回答 2

0

对于 iPhone:

在此处查看如何在 PHP 服务器上上传图像

还有这个上传图片

对于使用 Base64 编码将 UIImage 转换为字符串,请使用Base64 编码方法

我希望这会帮助你

于 2013-01-30T06:30:35.210 回答
0

您的 PHP 代码很好,问题出在 base 64 编码中。我已使用您的代码成功上传了一张图片,请执行以下操作以正确编码数据。

将此方法复制并粘贴到您的代码中(取自Creating a base-64 string from NSData

- (NSString*)base64forData:(NSData*) theData {
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

现在要获取编码的字符串

NSString *encodedString = [[self base64forData:data] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

附加编码字符串如下

[urlString appendFormat:@"image=%@", encodedString];
于 2013-11-16T21:24:32.737 回答