我正在研究iOS應用程序,並且在使用AFNetworking進行http請求時遇到了一些麻煩。AFNetworking EXC_BAD_ACCESS setCompletionBlock
當我運行代碼時,出現錯誤:EXC_BAD_ACCESS(code = 2 address = 0x0)。 當我嘗試設置CompletionBlock時發生錯誤。
我是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
謝謝!
您好Mattt,感謝回答。我添加了截圖,如果我在堆棧跟蹤的錯誤位置查找,請告訴我。這是我正在爲phonegap/cordova應用程序開發的插件的本地部分。所以它從javascript文件中調用,我傳入一個base64編碼的png作爲第二個參數。感謝您花時間幫助! – kevinstueber 2012-04-09 01:03:50
出於好奇,我對base64圖像字符串進行了硬編碼,而不是從方法參數中獲取它,並得到相同的錯誤。當我註釋掉'操作setCompletionBlock ...'部分時,錯誤不會發生...... – kevinstueber 2012-04-09 01:48:24