ios AfNetWoring 上传多张图片

来源:互联网 发布:多功能网络表 编辑:程序博客网 时间:2024/05/24 15:39

AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManagermanager];

        //设置返回的数据解析格式

        manager.responseSerializer.acceptableContentTypes= [NSSet setWithObject:@"text/html"];

        //以下为上传的一些参数,根据服务器网址要求,可以有可以没有

        NSString*uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"myUid"];

        NSString *valueWWW= [NSStringstringWithFormat:@"{"uid":"%@"}",uid];

        NSMutableDictionary*ssdict = [[NSMutableDictionary alloc] init];

        [ssdict setValue:valueWWWforKey:@"home"];

        

        //post方法进行图片上传

        AFHTTPRequestOperation*operation = [manager POST:@"http://public.aiweiker.com/index.php?g=App&m=HomePage&a=updateBg"parameters:ssdictconstructingBodyWithBlock:^(id<</span>AFMultipartFormData> formData) {

            

            //将图片装换为二进制格式--UIImageJPEGRepresentation第一个参数为要上传的图片,第二个参数是图片压缩的倍数

//如果要上传多张图片把下面两句代码放到for循环里即可

for (int i =0; i<</span>0; i++) {

       NSData *imageData =UIImageJPEGRepresentation(image0.1); 

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

    }           

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

            //成功的返回--进行你的操作

            NSDictionary *dict = responseObject;


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

            //失败的返回

            UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:nilmessage:@"请检查网络" delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];

            [alert show];

            [alert release];

            

        }];

        //获得上传进度

        [operation setUploadProgressBlock:^(NSUInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite) {

            NSLog(@"百分比:%f",totalBytesWritten*1.0/totalBytesExpectedToWrite);

 

        }];

1 0
原创粉丝点击