我正在开发一个 iOS 应用程序,并且在使用 AFNetworking 发出 http 请求时遇到了一些问题。
当我运行代码时,出现错误:EXC_BAD_ACCESS(code=2 address=0x0)。当我尝试 setCompletionBlock 时发生错误。
我是 Objective-C 的新手,这让我很困惑。
先感谢您。感谢大家的帮助!
#import "AFNetworking.h"
#import <Cordova/CDV.h>
#import "UploadImg.h"
@implementation UploadImg
- (void) uploadImg:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSURL *url = [NSURL URLWithString:@"http://test.com/mobile/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = [NSData dataFromBase64String:[arguments objectAtIndex:1]];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"TEST_STYLE" forKey:@"styleType"];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"imageName" fileName:@"image.png" mimeType:@"image/png"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error");
}];
[operation start];
}
@end
再次感谢!