2013-03-07 6 views
3

iPhone의 갤러리에서 JSON으로 이미지를 서버에 업로드하려면 어떻게해야합니까? 나는 시도 다음iOS의 JSON으로 갤러리에서 서버로 이미지를 업로드하는 방법

[Loading startLoading:YES]; 
NSString *urlString = [NSString stringWithFormat:@"http://railsboxtech.com/singing/jsn_song/register_response.php?accesstoken=%@",Access_Token]; 
//username, password, name, email, country 
NSString *parameter = [NSString stringWithFormat:@"username=%@&password=%@&fname=%@&email=%@&lname=%@&btnImage4=%@",userField.text,passField.text,fnameField.text,emailField.text,lnameField.text,btnImage4]; 
NSConnection *conn = [[NSConnection alloc] initWithDelegate:self]; 
[conn sendRequest:urlString withParaMeter:parameter withMethod:@"POST" withTag:1]; 
[conn startAsynchronousRequest]; 

답변

7

당신은, Base64로에 이미지 데이터를 변환해야합니다 당신은 당신의 위의 방법을 사용할 수있는 문자열로 이미지를 업로드하려면 : - base64로 문자열로 변환

을 자세한 내용은 this Stack Overflow answer을 참조하십시오. 그런 다음 코드를 사용하여 업로드 할 수 있습니다.

또 다른 방법

당신이 직접 이렇게 같은 이미지를 업로드 할 수 있습니다 :

-(void)uploadImage 
{  
    NSData *imageData = UIImagePNGRepresentation(yourImage); 

    NSString *urlString = [ NSString stringWithFormat:@"http://yourUploadImageURl.php?intid=%@",1]; 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [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"]; 

    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", 1]] 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]]; 
    [request setHTTPBody:body]; 

    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
} 
관련 문제