0
我已經下載從蘋果photopicker這裏的示例代碼蘋果photopicker上傳:http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010196-Intro-DontLinkElementID_2用PHP拍攝照片到MySQL數據庫
我已經修改了代碼,表示拍攝的照片的結果(節抓拍按鈕)在MyViewController.m:
// as a delegate we are being told a picture was taken
- (void)didTakePicture:(UIImage *)picture
{
[self.capturedImages addObject:picture];
}
然後用這一個類似的一個代碼,將其推出到我的Web服務器:
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;
PHP代碼的upload.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'] ;
}
?>
感謝
雷吉斯
更多關於[AFNetworking](https://github.com/AFNetworking/AFNetworking) – TUNER88