使用AFnetworking上传图片没有反应 [英] Upload image using AFnetworking did not response anything

查看:248
本文介绍了使用AFnetworking上传图片没有反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题出在这里:


  1. 我想在从图像选择器中选择完图像后立即上传图像。

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

因此,我将代码通过imagepicker委托方法中的afnetworking上传。

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

但它没有

//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.

推荐答案

最佳简便方法

请在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);

    }];

这篇关于使用AFnetworking上传图片没有反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆