AFNetWorking同时上传多张图片方式

1.

/**
 *  上传多张照片
 *
 *  @param imageDic 字典数组
 */
- (void)sendTopicImageToServer:(NSMutableDictionary *)imageDic{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [manager POST:SEND_TOPIC_IMAGE parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        //图片上传
        if (imageDic.count > 0) {
            for (NSString *key in [imageDic allKeys]) {
                [formData appendPartWithFileData:[imageDic objectForKey:key] name:key fileName:[NSString stringWithFormat:@"%@.png",key] mimeType:@"image/jpeg"];
            }
            [self initMBProgress:@"图片上传中..."];
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self setMBProgreeHiden:YES];
        self.imagesArray = [responseObject objectForKey:@"Detail"];
        
        /**
         *  发布图片和内容到服务器
         */
        if (sendTopicView.titleTextField.text.length<1) {
            [self initMBProgress:@"标题不能为空" withModeType:MBProgressHUDModeText afterDelay:1];
            return;
        }
        
        SharedInfo *sharedInfo = [SharedInfo sharedDataInfo];
        NSDictionary *params = @{@"Method":@"AddPostInfo",@"RunnerUserID":sharedInfo.user_id,@"RunnerIsClient":@"1",@"RunnerIP":@"2",@"Detail":@[@{@"ClassID":self.cate_id,@"Name":sendTopicView.titleTextField.text,@"IsShow":@"1",@"Detail":sendTopicView.contentTextView.text,@"Sort":@"",@"UserID":sharedInfo.user_id,@"IP":@"",@"ProvinceID":isStrEmpty(sharedInfo.area)?@"1":sharedInfo.area,@"CityID":isStrEmpty(sharedInfo.city)?@"1":sharedInfo.city}],@"Images":self.imagesArray};
        
        [CKHttpRequest createRequest:HTTP_COMMAND_SEND_TOPIC WithParam:params withMethod:@"POST" success:^(id result) {
            if (result && [[result objectForKey:@"Success"]intValue] > 0) {
                [self.navigationController dismissViewControllerAnimated:YES completion:^{
                    [self initMBProgress:@"发布成功" withModeType:MBProgressHUDModeText afterDelay:1];
                }];
            }else{
                [self initMBProgress:[result objectForKey:@"Msg"] withModeType:MBProgressHUDModeText afterDelay:1.5];
            }
            
        } failure:^(NSError *erro) {
            
        }];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}


2、将数组中UIimage对象转换成NSData对象,然后传给上面的方法,最后将转换出来的NSData对象上传到服务器
- (void)sendTopicAction{
    for (int i = 0; i < _locaPhotoArr.count; i ++) {
        NSDictionary *dict = [_locaPhotoArr objectAtIndex:i];
        UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
        
        NSString *indexStr = [NSString stringWithFormat:@"picturename%d",i];
        NSData *imageData = UIImageJPEGRepresentation(image, 0.0f);
        [Exparams addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:imageData,indexStr, nil]];
    }
    [self sendTopicImageToServer:Exparams];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值