How to Upload Images Using AFNetworking directly f

2019-06-23 22:25发布

问题:

I am totally new to iOS.I want to upload images to my server using AFNetworking. I tried it but it still not done. Please Help me, thanks in advance

回答1:

try with this its work for me

-(void) uplodeImages{

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager   alloc] initWithBaseURL:[NSURL URLWithString:@"url where to upload images"]];
    NSData *imageData = UIImagePNGRepresentation([UIImage    imageNamed:@"cat1.png"]);
    NSDictionary *parameters = @{@"username": @"", @"password" : @""};
    AFHTTPRequestOperation *op = [manager POST:@"cat1.png" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"cat1.png" mimeType:@"image/png"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];
    [op start];
}