我尝试使用图像发送Post Request时出错

时间:2015-03-18 22:07:52

标签: ios objective-c post

当我尝试使用图像发送POST请求时,我收到此错误

线程1:信号SiGABART

我认为这部分

[formData appendPartWithFileData:[NSData dataWithData:[self.images objectAtIndex:indexScroll]] name:@"FileUploadPost" fileName:@"image.jpg" mimeType:@"image/jpeg"]; 

导致错误

- (IBAction)uploadPhoto:(id)sender {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer new];

        NSDictionary *parameters = @{@"HI": @"Hello"};
        [manager POST:@"http://requestb.in/1c69jt31" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:[NSData dataWithData:[self.images objectAtIndex:indexScroll]] name:@"FileUploadPost" fileName:@"image.jpg" mimeType:@"image/jpeg"];

            [formData appendPartWithFileData:[self.images objectAtIndex:indexOfPage] name:@"FileUploadPost" fileName:@"image.jpg" mimeType:@"image/jpeg"];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSData * data = (NSData *)responseObject;
            NSLog(@"Success,Response string: %@", [NSString stringWithCString:[data bytes] encoding:NSISOLatin1StringEncoding]);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];

    }

1 个答案:

答案 0 :(得分:0)

如果self.imagesUIImage个对象的数组,则必须使用UIImageJPEGRepresentationUIImagePNGRepresentation之类的内容来提取NSData UIImage object:

- (IBAction)uploadPhoto:(id)sender {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer new];

    NSDictionary *parameters = @{@"HI": @"Hello"};
    [manager POST:@"http://requestb.in/1c69jt31" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:UIImageJPEGRepresentation(self.images[indexScroll], 0.8)] name:@"FileUploadPost" fileName:@"image.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Success,Response string: %@", responseString);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}