简体   繁体   中英

Upload image using AFnetworking did not response anything

Here's the problem :

  1. I want to upload image immediately after finish choosing image from imagepicker.

  2. So, I put my code to upload image using afnetworking in imagepicker delegate method.

  3. But it doesn't response anything.

     //set the imageview to current image after choosing profilePic.image = image; //dismiss the imagepicker [picker dismissModalViewControllerAnimated:YES]; //start upload image code NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"steventi1901", @"username", UIImagePNGRepresentation(profilePic.image), @"profile_pic", nil]; AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:updateProfile]; NSData *imageToUpload = UIImagePNGRepresentation(profilePic.image); NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"PUT" path:@"" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { [formData appendPartWithFileData: imageToUpload name:@"file" fileName:@"temp.png" mimeType:@"image/png"]; //NSLog(@"dalem"); }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *response = [operation responseString]; NSLog(@"response: [%@]",response); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if([operation.response statusCode] == 403){ NSLog(@"Upload Failed"); return; } NSLog(@"error: %@", [operation error]); }]; [operation start]; //end upload image code 

It really didn't response anything so i didn't know if the process fail or success.

Best and Easy way to upload Image using Afnetworking.

Please use below AFNetworking.

pod 'AFNetworking', '~> 2.5.4'

//Create manager

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    //parameters if any
    NSMutableDictionary *AddPost = [[NSMutableDictionary alloc]init];

    [AddPost setValue:@"Addparma" forKey:@"param"];

    NSString * url = [NSString stringWithFormat:@"www.addyourmainurl.com"];;    

    NSLog(@"AddPost %@",AddPost);

    [manager POST:url parameters:[AddPost copy] constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        //add image data one by one

        for(int i=0; i<[Array count];i++)
        {
            UIImage *eachImage  = [Array objectAtIndex:i];

            NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);

            [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"image%d",i] fileName:[NSString stringWithFormat:@"image%d.jpg",i ] mimeType:@"image/jpeg"];

        }  

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"Success: %@", responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@", error);

    }];

您设置了选择器的代表吗?

[yourPicker setDelegate:self]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM