我正在使用类似iPhone的应用程序。根据我的要求,我需要发送相机捕获的图像到服务器使用PHP网址。我将捕获的图像存储在应用程序资源文档目录中。拍摄照片并将其发送到iPhone中的PHP服务器
这里是我的PHP代码 -
$file_path = "../Gallery/";
$file_path = $file_path . basename($_FILES['Documents']['name']);
if(move_uploaded_file($_FILES['Documents']['tmp_name'], $file_path)) {
echo "Image is upload";
} else{
echo "Image is not Upload";
}
这里是我的iPhone的代码 -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGFloat compression = 0.5f;
UIImage *smallSizeImage = [self scaleImage:image toSize:CGSizeMake(140.0, 80.0)];
imageData = UIImageJPEGRepresentation(smallSizeImage, compression);
ivPicture.image = smallSizeImage;
self.passingNbPosition.positionImageId = [Util getNewGUID];
[imageData writeToFile:[Util getFilePathForFileName:[NSString stringWithFormat:@"%@.jpg",self.passingNbPosition.positionImageId]] atomically:YES];
// setting up the URL to post to
NSString *urlString = @"http://...........php";
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", self.passingNbPosition.positionImageId] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
}
使用上面的代码在控制台上打印时,我得到的回报数据,但返回的字符串来了之后,“图片不上传“。我不知道我的错误在哪里。我认为这是我的iPhone代码中的一个问题。有人可以帮助我,我在这里做错了。
谢谢..
你听说过'AFNetworking'?试试这个第三方库打网址'https:// github.com/AFNetworking/AFNetworking' – Mani
谢谢,但我想知道我的上述代码中的错误在哪里? –
@KumarKlYes队友..我越来越。 –