我正在尝试使用AFNetwork上传一张NSData图像,但出现了问题我无法找到错误的地方
这是我的代码:
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"test.jpg"], 0.1);
NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"this is only a test post for \"app\".", @"app",
@"this POST for \"test\". ", @"test", nil];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://url/upload.php" parameters:postParm constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" ];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
崩溃的结果如下:
*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -streamStatus仅为抽象类定义。定义 - [AFMultipartBodyStream streamStatus]!' * 第一次抛出调用堆栈: ( 0 CoreFoundation 0x019d91e4 exceptionPreprocess + 180 1 libobjc.A.dylib 0x017588e5 objc_exception_throw + 44 2 CoreFoundation 0x019d8fbb + [NSException raise:format:] + 139 3基础0x01433b3b NSRequestConcreteImplementation + 218 4基础0x0143b746 - [NSStream(NSStream)streamStatus] + 48 5 readWritePlist 0x000333a1 - [AFMultipartBodyStream open] + 49 6 CoreFoundation 0x0199ac44 CFReadStreamOpen + 116 7 CFNetwork 0x026f395d _ZN30CoreReadStreamFromCFReadStream16_streamImpl_OpenEP13CFStreamErrorPh + 53 8 CFNetwork 0x027c5ae6 _ZThn60_N30CoreReadStreamFromCFReadStream16_streamImpl_OpenEP13CFStreamErrorPh + 34 9 CFNetwork 0x026d1112 _ZN14CoreStreamBase21_streamInterface_OpenEv + 72 10 CFNetwork 0x026f2c34 _ZN17HTTPNetConnection19prepareTransmissionEP17HTTPNetStreamInfoP17__CoreWriteStream + 1614 11 CFNetwork 0x026f2063 _ZN17HTTPNetConnection19requestStateChangedEPvN13NetConnection5StateEP13CFStreamError + 415 12 CFNetwork 0x026f21fd _ZN13NetConnection18scheduleNewRequestEP13_CFNetRequestS1_h + 293 13 CFNetwork 0x026f1da4 _ZN13NetConnection7enqueueEPvh + 538 14 CFNetwork 0x026f1b26 _ZN17HTTPNetStreamInfo16_streamImpl_OpenEP13CFStreamErrorPh + 104 15 CFNetwork 0x02756846 _ZThn60_N17HTTPNetStreamInfo16_streamImpl_OpenEP13CFStreamErrorPh + 34 16 CFNetwork 0x026d1112 _ZN14CoreStreamBase21_streamInterface_OpenEv + 72 17 CFNetwork 0x026f1315 _ZN12HTTPProtocol10openStreamEv + 267 18 CFNetwork 0x0276498c _ZN12HTTPProtocol26useNetConnectionForRequestEP13NetConnectionP15__CFHTTPMessageh + 1550 19 CFNetwork 0x026f05d8 _ZN24HTTPConnectionCacheEntry28dispatchConnectionToProtocolEP13NetConnectionP12HTTPProtocolP18HTTPRequestMessageh + 284 20 CFNetwork 0x026f03b9 _ZN24HTTPConnectionCacheEntry34notifyNextProtocolOfOpenConnectionEP13NetConnectionh + 349 21 CFNetwork 0x026ed71d _ZN24HTTPConnectionCacheEntry25enqueueRequestForProtocolEP12HTTPProtocolP15__CFHTTPMessage + 637 22 CFNetwork 0x026ed0db _ZN19HTTPConnectionCache34_onqueue_enqueueRequestForProtocolEP12HTTPProtocolP15__CFHTTPMessage + 185 23 CFNetwork 0x0276a897 ___ZN19HTTPConnectionCache25enqueueRequestForProtocolEP12HTTPProtocolP15__CFHTTPMessage_block_invoke + 35 24 CoreFoundation 0x0197ac69 CFArrayApplyFunction + 57 25 CFNetwork 0x026e8441 _ZN19RunloopBlockContext7performEv + 155 26 CFNetwork 0x027ca3f4 _ZThn16_N19RunloopBlockContext24multiplexerClientPerformEv + 20 27 CFNetwork 0x026e8257 _ZN17MultiplexerSource7performEv + 299 28 CFNetwork 0x026e806c _ZN17MultiplexerSource8_performEPv + 76 29 CoreFoundation 0x0196277f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 15 30 CoreFoundation 0x0196210b CFRunLoopDoSources0 + 235 31 CoreFoundation 0x0197f1ae __CFRunLoopRun + 910 32 CoreFoundation 0x0197e9d3 CFRunLoopRunSpecific + 467 33 CoreFoundation 0x0197e7eb CFRunLoopRunInMode + 123 34基础0x01353ece + [NSURLConnection(Loader)_resourceLoadLoop:] + 381 35基础0x013afa07 - [NSThread main] + 76 36基金会0x013af966 __NSThread__main + 1275 37 libsystem_pthread.dylib 0x0241d5fb _pthread_body + 144 38 libsystem_pthread.dylib 0x0241d485 _pthread_struct_init + 0 39 libsystem_pthread.dylib 0x02422cf2 thread_start + 34 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止
任何想法在这里我的错误是什么..!?
答案 0 :(得分:6)
我将AFNetwork更新到2.3.1的最新版本后,问题就解决了。