Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 正在将图像上载到json服务器,并发送空NSUrlRequest_Iphone_Json - Fatal编程技术网

Iphone 正在将图像上载到json服务器,并发送空NSUrlRequest

Iphone 正在将图像上载到json服务器,并发送空NSUrlRequest,iphone,json,Iphone,Json,我正在尝试将图像上载到json服务器,但url请求变为空。下面是我正在使用的代码 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *imageToScale=[info objectForKey:UIImagePickerControllerOriginalImage]; UIImage *img

我正在尝试将图像上载到json服务器,但url请求变为空。下面是我正在使用的代码

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

UIImage *imageToScale=[info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *img = imageToScale;
//NSLog(@"finalImage---------------------------------%@",self.finalImage);
NSData *imageData = UIImageJPEGRepresentation(img, 0.40);

profileImg = profilePic;

NSArray *arrImg = [profileImg componentsSeparatedByString:@"/"];
NSString *strImg1 = [arrImg objectAtIndex:[arrImg count]-2];
NSString *strImg2 = [arrImg lastObject];
NSLog(@"img2-----------------%@",strImg2);
NSString *pImg = [NSString stringWithFormat:@"%@%@",strImg1, strImg2];
NSLog(@"img-----------------%@",pImg);
NSString *urlString = [NSString stringWithFormat:@"http://www.funnyghg.co/overheadPost.php?userId=%@&type=Photo&text=Demo test&image=%@&location=Chandigarh&Upload=Upload", uid, pImg];
NSLog(@"url :%@", urlString);
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[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"];

/*
 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=\"1.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]];
// 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------------%@",returnString);

[self dismissModalViewControllerAnimated:YES];
[appDelegate showAllNavItems];
[appDelegate navFrame];

if([returnString isEqualToString:@"true"])
{

    if([profile isEqualToString:@"friend"])
    {
        [self callServerFriend];
    }
    if([profile isEqualToString:@"public"])
    {
        [self callServerPublic];
    }
}
}

请为以上内容提供指导。提前感谢。

这是因为在您的代码中,演示和测试之间的urlString中有一个空格。请将其按原样删除

之前:

NSString *urlString = [NSString stringWithFormat:@"http://www.funnyghg.co/overheadPost.php?userId=%@&type=Photo&text=Demo test&image=%@&location=Chandigarh&Upload=Upload", uid, pImg];
NSLog(@“url:%@”,urlString)

之后:

NSString *urlString = [NSString stringWithFormat:@"http://www.funnyghg.co/overheadPost.php?userId=%@&type=Photo&text=Demotest&image=%@&location=Chandigarh&Upload=Upload", uid, pImg];
NSLog(@“url:%@”,urlString)


我希望它现在能起作用。

检查我的答案嘿,谢谢,这是原因,但在我的代码中它仍然不起作用,在您的代码中,它在控制台中给出的消息为true,但在服务器上,图像没有得到post,只有文本得到post。。