2010-12-27 2 views
4

이 웹 서비스를 사용하여 웹 서비스 및 iOS 응용 프로그램을 만들고 있습니다. 사진 데이터 자체가이 문제로 HTML과 이제 다른 POST 데이터와 함께 UIImage에서 JPEG로 이미지 업로드

하지

  1. picture[title] 사진
  2. picture[picture]의 제목 : 웹 서비스의 API는 두 개의 POST-변수와 HTTP POST 요청을 받아 웹 브라우저는 요청을 구성하고 걱정해야 할 것은 enctype="multipart/form-data"입니다.

    하지만 iOS 앱에는 두 가지 문제가 있습니다. 두 가지 질문과 함께 지금 당장 물어 보겠습니다.

    1. UIImage를 이미지 픽커 또는 카메라에서 원시 JPEG 데이터로 변환하는 방법은 무엇입니까?
    2. picture[title] -field를 일반 텍스트로, picture[picture] 필드를 JPEG 데이터와 함께 포함해야하는 올바른 POST 데이터로 NSURLRequest를 만드는 방법은 무엇입니까? 서버 측에서는 PHP의 $_FILES과 비슷한 Paperclip 젬을 사용합니다.

    이미지를 업로드하는 몇 가지 예를 보았습니다. 그러나 여기에는 이미지 만 포함되며 다른 POST 변수는 포함되지 않습니다.

    아무도 도와 줄 수 있습니까? 감사.

+0

이것은 잘 쓰여진 질문이지만 "ios"또는 "objective-c"태그가 지정되지 않았기 때문에 오랜 시간 동안 답변을 얻지 못했을 것입니다. 그 태그를 추가 할 수 있습니까? 아직 수행 할 수있는 권한이 없기 때문입니다. –

답변

4

체크 아웃 꽤 좋은 튜토리얼 : 그것에 대해 매우 옳지 않아

http://iphone.zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

것은 실제로 float로서 품질을 취 UIImageJPEGRepresentation에 품질 설정으로 (90)을 통과한다는 것입니다 0.0에서 1.0 사이이므로 0.9가되어야합니다. 코드가 작동한다고 확신합니다. 의도 한대로 90 % 대신 100 % 품질을 지정했습니다. 그리고, 단지 편의를 위해와 휴식을 연결할 경우, 여기 (명확성을 위해 리팩토링과 더 나은 질문에 맞게) 관련 코드 : 그것은 여기에 쓰여 나는 실제로 그것을 컴파일하지 않은 아마 그래서

- (void)uploadImage:(UIImage *)image toURL:(NSURL *)url withTitle:(NSString *)title { 

    // encode the image as JPEG 
    NSData *imageData = UIImageJPEGRepresentation(image, 0.9); 

    // set up the request 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:url]; 

    // create a boundary to delineate the file 
    NSString *boundary = @"14737809831466499882746641449"; 
    // tell the server what to expect 
    NSString *contentType = 
    [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    // make a buffer for the post body 
    NSMutableData *body = [NSMutableData data]; 

    // add a boundary to show where the title starts 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] 
    dataUsingEncoding:NSASCIIStringEncoding]]; 

    // add the title 
    [body appendData:[ 
    @"Content-Disposition: form-data; name=\"title\"\r\n\r\n" 
    dataUsingEncoding:NSASCIIStringEncoding]]; 
    [body appendData:[title 
    dataUsingEncoding:NSASCIIStringEncoding]]; 

    // add a boundary to show where the file starts 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] 
    dataUsingEncoding:NSASCIIStringEncoding]]; 

    // add a form field 
    [body appendData:[ 
    @"Content-Disposition: form-data; name=\"picture\"; filename=\"image.jpeg\"\r\n" 
    dataUsingEncoding:NSASCIIStringEncoding]]; 

    // tell the server to expect some binary 
    [body appendData:[ 
    @"Content-Type: application/octet-stream\r\n" 
    dataUsingEncoding:NSASCIIStringEncoding]]; 
    [body appendData:[ 
    @"Content-Transfer-Encoding: binary\r\n" 
    dataUsingEncoding:NSASCIIStringEncoding]]; 
    [body appendData:[[NSString stringWithFormat: 
    @"Content-Length: %i\r\n\r\n", imageData.length] 
    dataUsingEncoding:NSASCIIStringEncoding]]; 

    // add the payload 
    [body appendData:[NSData dataWithData:imageData]]; 

    // tell the server the payload has ended 
    [body appendData: 
    [[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] 
    dataUsingEncoding:NSASCIIStringEncoding]]; 

    // add the POST data as the request body 
    [request setHTTPMethod:@"POST"]; 
    [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); 
} 

몇 가지 문제가 있습니다.

PHP에서는 $ _REQUEST [ 'title'] 및 $ _FILES [ 'picture']가 설정되어 있어야합니다.

관련 문제