Sending UIImage from UIImagePickerController to PHP server using ASIFormDataRequest
在当前的项目中,我实现了一个
这一切都很好,花花公子..但是,我上传图像时遇到问题。
一旦我从
但是,如果我选择项目中存在的任何其他图像(例如某些背景图像),上传过程将完美运行!这是奇怪的部分。一旦我使用 UIImagePickerController 中的图像,它就不起作用......即使我确实得到了图像(参见代码中的 addSubview,它有效)。
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | @interface ServiceViewController () @property (nonatomic) UIImage *image; [...] @end @implementation ServiceViewController @synthesize image = _image; [...] - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { self.image = [[info objectForKey:UIImagePickerControllerOriginalImage] copy]; [self.tableView reloadData]; [self dismissModalViewControllerAnimated:YES]; } - (void)sendToApi { // Send post data NSString *urlString = [NSString stringWithFormat:@"%@/%@", API_BASE_DOMAIN2, @"requestservice"]; urlString = @"http://localhost/api/public_html/requestservice"; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url]; [request setUseKeychainPersistence:YES]; [request setPostValue:[self.problems objectAtIndex:self.problem] forKey:@"problem"]; [request setPostValue:[self stationName] forKey:@"station"]; [request setPostValue:self.otherInformation forKey:@"information"]; [request setPostValue:[NSNumber numberWithInt:self.lock] forKey:@"lock"]; [request setUploadProgressDelegate:self]; [request setDidFinishSelector:@selector(didCompleteRequestService:)]; [request setDidFailSelector:@selector(processFailed:)]; [request setDelegate:self]; [request setRequestMethod:@"POST"]; // Upload an image [request addData:[NSData dataWithData:UIImageJPEGRepresentation(self.image, 0.9)] withFileName:@"img.jpg" andContentType:@"image/jpeg" forKey:@"picture"]; UIImageView *view = [[UIImageView alloc] initWithImage:self.image]; [view sizeToFit]; [self.view addSubview:view]; [request startAsynchronous]; } - (void)setProgress:(float)newProgress { // [prgressView setProgress:newProgress]; NSLog(@"New progress: %f", newProgress); } - (void)didCompleteRequestService:(ASIHTTPRequest *)request { NSLog(@"Request completed"); NSLog(@"Result: %@", request.responseString); } |
服务器端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var_dump($_FILES); if ($this->_aPicture !== null) { $sTarget = DIR_SERVICE . $this->_sFileNamePrefix .".jpg"; var_dump($this->_aPicture); var_dump($sTarget); if (!move_uploaded_file($this->_aPicture['tmp_name'], $sTarget)) { $this->returnHttpError(500); } } $this->_writeDataToFile(); |
这是我的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 2013-03-21 00:04:04.859 AppName[15598:c07] New progress: 0.000000 2013-03-21 00:04:04.862 AppName[15598:c07] New progress: 1.000000 2013-03-21 00:04:04.863 AppName[15598:c07] Request completed 2013-03-21 00:04:04.863 AppName[15598:c07] Result: array(1) { ["picture"]=> array(5) { ["name"]=> string(7)"img.jpg" ["type"]=> string(0)"" ["tmp_name"]=> string(0)"" ["error"]=> int(1) ["size"]=> int(0) } } array(5) { ["name"]=> string(7)"img.jpg" ["type"]=> string(0)"" ["tmp_name"]=> string(0)"" ["error"]=> int(1) ["size"]=> int(0) } string(58)"/Users/paulp/Sites/api/service/K98XQPo7zx.jpg" Sending error: 500 |
我尝试了很多不同的东西,例如:
- 如何上传从 UIImagePickerController 拍摄的图像
-
上传使用 UIImagePickerController 选择的图片
还有一些。
我没有改成例如MKNetworkKit,因为(目前)没有时间或预算,所以这不是解决方案。
我该如何解决这个问题?
保存选中的图片
1 2 3 4 5 6 7 | - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { //Save selected image to document directory jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; [UIImageJPEGRepresentation(img, 1.0) writeToFile:jpgPath atomically:YES]; [picker dismissModalViewControllerAnimated:YES]; } |
然后使用
1 2 3 4 | ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; if (jpgPath.length > 0) { [request setFile:jpgPath forKey:@"image"]; } |
最后,我以前的代码运行良好......
结果是我的 PHP 设置不正确。在我的
试试这个代码,它非常适合我:
1 | [request addData:UIImagePNGRepresentation(image) withFileName:@"image.png" andContentType:@"multipart/form-data" forKey:@"image"]; |