使用AFnetworking上传图像没有任何响应

时间:2013-03-07 04:44:31

标签: ios uiimagepickercontroller afnetworking image-uploading

问题在于:

  1. 我想在完成从imagepicker中选择图片后立即上传图片。

  2. 所以,我把我的代码用于在imagepicker委托方法中使用afnetworking上传图片。

  3. 但它没有任何反应。

    //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
    
  4. 它确实没有回应,所以我不知道这个过程是否失败或成功。

2 个答案:

答案 0 :(得分:1)

使用Afnetworking上传图片的最佳方式。

请使用以下AFNetworking。

pod&#39; AFNetworking&#39;,&#39;〜&gt; 2.5.4&#39;

//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);

    }];

答案 1 :(得分:0)

您是否设置了选择器的委托?

[yourPicker setDelegate:self]