2012-12-12 1 views
0

Rubymotion을 사용하여 iOS 앱을 만들고 있습니다. 이미지의 멀티 파트 업로드 을 만들려고합니다.AFNetworking을 사용하여 이미지를 업로드 할 수 없습니다.

내가 업로드를 만들기 위해이 코드를 사용하지만 난 얻을 오류 :

내가 무엇을 볼 수에서
undefined method `setCompletionBlockWithSuccess' for #<AFJSONRequestOperation:0xb227f00> (NoMethodError) 

, 작업이 유효한 대상은 다음과 같습니다

data = {token: "2xCGdzcuEeNzhst3Yaa8f", task: 1, message: "message", latitude: 1, longitude: 1} 

    client = AFHTTPClient.alloc.initWithBaseURL(NSURL.URLWithString("http://api.example.com/api/v1/")) 

     request = client.multipartFormRequestWithMethod('POST', path:"messages", parameters:data, constructingBodyWithBlock:lambda{ |form_data| 
      image_data = UIImagePNGRepresentation(image.image) 
      form_data.appendPartWithFileData(image_data, name:'new_avatar', fileName:'new_avatar.png', mimeType:'image/png') 
     }) 

     operation = AFJSONRequestOperation.alloc.initWithRequest(request) 
     operation.setCompletionBlockWithSuccess(lambda{ |operation, responseObject| puts 'all done!'}) 

나는이 오류 이 방법이 있어야합니다. 여기에 뭔가가 빠졌습니까?

업데이트 1

이 내 업데이트 된 코드는하지만 난 그것을 실행할 때 POST 동작이 발생하지 않는 것 같습니다. 아직 코드가 누락 되었습니까? 어떤 오류도 발생하지 않습니다.

data = {token: "2xCGdzcuEeNzhs5tYaa8f", task: 1, message: "message", latitude: 1, longitude: 1} 

    client = AFHTTPClient.alloc.initWithBaseURL(NSURL.URLWithString("http://api.example.com/api/v1/")) 

    request = client.multipartFormRequestWithMethod('POST', path:"messages", parameters:data, constructingBodyWithBlock:lambda{ |form_data| 
     image_data = UIImagePNGRepresentation(image.image) 
     form_data.appendPartWithFileData(image_data, name:'new_avatar', fileName:'new_avatar.png', mimeType:'image/png') 
    }) 

    operation = AFJSONRequestOperation.alloc.initWithRequest(request) 
    operation.setCompletionBlockWithSuccess(lambda { |operation, responseObject| puts 'all done!'}, 
              failure: lambda { |operation, error| puts 'error' }) 

요청 개체의 출력이 :

<NSMutableURLRequest:195641792 - url: http://api.example.com/api/v1/messages, 
    headers: {"Content-Length"=>"118200", "Content-Type"=>"multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY", "Accept-Language"=>"en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8", "User-Agent"=>"theapp/1.0 (iPhone Simulator; iOS 6.0; Scale/1.00)"}, 
    cache policy: 0, Pipelining: false, main doc url: , timeout: 60.0, network service type: 0 > 

작업 개체의 출력이 :

<AFJSONRequestOperation:0xa78c660> 

답변

1

메소드 서명은 setCompletionBlockWithSuccess(lambda..., failure: lambda...)입니다. 오류 매개 변수를 추가해야합니다. 이런 식으로 뭔가가 :

operation.setCompletionBlockWithSuccess(lambda { |operation, responseObject| puts 'all done!'}, 
             failure: lambda { |operation, error| puts 'error' }) 

또한, 당신은 실제로 요청 작업을 시작해야합니다 할 수 있습니다 :

operation.start 

이 방법의 정의 https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFJSONRequestOperation.m#L102-L103를 참조하십시오.

+0

고마워요! 지금 오류 메시지를 없애고 POST 동작이 실행되지 않는 것 같습니다. 내 업데이트 된 질문을 참조하십시오. –

+0

실제로 요청을 시작 했습니까? 'operation.start' –

0

나는 이런 종류의 일을하는 클래스를 썼다. 내 대답 HERE을 확인하십시오. 그것은 AFNetworking을 사용하고 기본 네트워크 요청을위한 방법을 포함합니다.

+0

감사합니다.하지만 불행히도 Rubymotion (Ruby)을 사용하고 있으며 objective-c를 사용하지 않았습니다. 왜 내가이 오류가 발생하는지 어떤 생각? –

+0

Rakefile에서'pod "AFNetworking"을 사용하여 CocoaPods (motion-cocoapod)를 설치하고 AFNetworking을 RubyMotion 앱에 포함시킬 수 있습니다. CocoaPod에 대한 자세한 내용은 https://github.com/HipByte/motion-cocoapods를 참조하십시오. –

+0

예, AFmotion 보석과 함께 해당 포드를 사용하고 있습니다. 그 보석으로 직접 이미지를 업로드 할 방법을 찾지 못해서 원시 AF 코드를 시도하고 있습니다. –