【问题标题】:Apple photopicker upload taken picture with php to a mysql databaseApple photopicker 将使用 php 拍摄的照片上传到 mysql 数据库
【发布时间】:2013-01-21 18:08:01
【问题描述】:

我已经从 Apple 下载了 photopicker 的示例代码:http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010196-Intro-DontLinkElementID_2

我在 MyViewController.m 中修改了代表照片拍摄结果的代码部分(快照按钮):

// as a delegate we are being told a picture was taken
- (void)didTakePicture:(UIImage *)picture
{
    [self.capturedImages addObject:picture];
}

然后使用与此类似的代码将其推送到我的网络服务器:

NSData *imageData = UIImageJPEGRepresentation(**_capturedImages.images**, 90);
NSString *urlString = @"http://localhost/upload.php";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);

我遇到的问题是我不断收到消息说文件无法上传或代码崩溃。

我认为这只是正确访问生成的图像并正确推送它的问题。

因为我不知道如何从 MyViewController.m 中提取图像:

@property (nonatomic, retain) **NSMutableArray** *capturedImages;

upload.php 的 PHP 代码是:

<?php

    $file = basename($_FILES['userfile']['name']);
    $uploadFile = $file;
    $randomNumber = rand(0, 99999); 
    $newName = $randomNumber . $uploadFile;

    echo $file;

    echo $uploadFile;

    echo $randomNumber;

    echo $newName;

    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
    {
        echo "Temp file uploaded. \r\n";
    } 
    else 
    {
        echo "Temp file not uploaded. \r\n";
    }

    if ($_FILES['userfile']['size']> 300000) {
        exit("Your file is too large."); 
    }

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) 
    {
            $postsize = ini_get('post_max_size');   //Not necessary, I was using these
            $canupload = ini_get('file_uploads');    //server variables to see what was 
            $tempdir = ini_get('upload_tmp_dir');   //going wrong.
            $maxsize = ini_get('upload_max_filesize');

            echo "http://localhost/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
    }

?>

谢谢

雷吉斯

【问题讨论】:

    标签: php mysql xcode camera


    【解决方案1】:

    这是使用 AFNetworking 上传的好例子 http://www.6foot3foot.com/developer-journal/afnetworking-php

    【讨论】:

      猜你喜欢
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多