2014-01-20 3 views
0

사용자가 이미지를 서버에 업로드 할 때 진행률 표시 줄을 표시하려고합니다. 내 이미지가 php url을 사용하여 서버에 성공적으로 업로드되고 있습니다. 하지만이 NSUrlConnection 클래스를 사용하고 있지 않습니다. 실제로 다른 여러보기 컨트롤러 클래스에서 이미지를 여러 개 업로드하는, 그래서 이미지 데이터 & 이미지 ID 전달하는 일반 업로드 이미지에 대한 내 Util 클래스에서 메서드를 추가했습니다.iPhone - 이미지를 서버에 업로드 할 때 진행률 표시 줄을 표시하는 방법?

다음

내 백분율 클래스 방법입니다 : 그래서 난 내 아래의 경우에있는 NSURLConnection 위임을 구현할 수있는 방법을

내의 ViewController 클래스에서
+(void)uploadImageOnServer:(NSData *)imageData forId:(NSString *)imageId 
{ 
    NSString *urlString = @"http://url..............."; 

    // setting up the request object now 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 

    [request setURL:[NSURL URLWithString:urlString]]; 

    [request setHTTPMethod:@"POST"]; 

    NSString *boundary = @"---------------------------147378098314876653456641449"; 

    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]]; 

    NSString *stringData = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Documents\"; filename=\%@.jpg\r\n",imageId]; 

    [body appendData: [stringData 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]; 
} 

, 나는 같은 서버에 이미지를 업로드하는 방법은 위의 호출 오전 :

[Util uploadImageOnServer:positionData forId:positionImageId]; 

하나의 이미지를 서버에 업로드하는 데 약 4-5 초가 소요됩니다. 첨부 된 스크린 샷과 같은 진행보기를 추가하고 싶습니다. 얼마나 많은 데이터가 업로드되었는지 사용자에게 표시하기 위해 일반적인 NSUrlConnection 대리자를 Util 클래스에 통합 할 수 있습니까?

감사합니다.

enter image description here

답변

1

당신은 비동기 연결과 connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: 위임 방법을 사용해야합니다.

또한 AFNetworking 및 제공되는 진행률 콜백 사용을 고려해야합니다.

+0

감사합니다. @ WainMy 문제는 이미지를 업로드하기 위해 Util 메서드를 호출 할 때마다 발생하므로 doSendBodyData NSUrlConnection 대리자 메서드를 호출하려면 어떻게해야합니까? 내 코드로 나를 안내 할 수 있습니까? –

+0

연결은 메서드를 호출 할 것이므로 'Util'과 같은 정보를 사용하는 방법을 제공해야합니다.이 매개 변수는 진행을 매개 변수로 취하는 블록 매개 변수를 취합니다 (기본적으로 AFNetworking 인터페이스입니다). – Wain

+0

이 Util 메서드에 대해 URLWithString을 사용하여 NSURLRequest를 어떻게 얻을 수 있습니까? 다음 나는 단지 didReceiveData 대리자 메서드에서 업로드 비율을 얻을 수 있습니다. 내 Util mehod 서버 성공 또는 실패 메시지를 반환하는 중입니다. –