AFN上传多张图片到服务器

87 篇文章 1 订阅
第一步//获取图片
  
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"添加照片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [actionSheet dismissViewControllerAnimated:YES completion:^{
            
            
            
        }];
    }];
    
    
    UIAlertAction *PhotoAlbum = [UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
        [self reloadImagesFromLibrary];
        
        
    }];
    
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"从摄像头获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
            
            picker.mediaTypes = temp_MediaTypes;
            
            picker.delegate = self;
            
            picker.allowsEditing = YES;
            
        }
        
        
        [self presentViewController:picker animated:YES completion:^{
            
        }];
        
        
    }];
    
    [actionSheet addAction:PhotoAlbum];
    [actionSheet addAction:camera];
    [actionSheet addAction:cancel];
    
    
   [self presentViewController:actionSheet animated:YES completion:^{
       
   }];



//这个是调用摄像头的回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    [self dismissViewControllerAnimated:YES completion:nil];

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    [self.frontIDCard setImage:[image imageWithRenderingMode:1] forState:UIControlStateNormal];

    //因为要上传多张照片所以放到一个数组里 (要注意控制图片的尺寸和质量)
    [self.postImageArr addObject:[self image:image byScalingToSize:CGSizeMake(400, 800)]];
   
}

//这个是选取拍摄的照片的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//更改图片大小
- (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    
    UIGraphicsBeginImageContext(targetSize);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width  = targetSize.width;
    thumbnailRect.size.height = targetSize.height;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage ;
}

//传图片流
- (void)postImages {
    

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
  
    [manager POST:Period parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
         // 上传 多张图片
         for(NSInteger i = 0; i < self.postImageArr.count; i++) {
        
             
             NSData * imageData = UIImageJPEGRepresentation([self.postImageArr objectAtIndex: i], 0.5);
             // 上传的参数名
             
             NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
             formatter.dateFormat = @"yyyyMMddHHmmss";
             NSString *str = [formatter stringFromDate:[NSDate date]];
             NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
             [formData appendPartWithFileData:imageData name:str fileName:fileName mimeType:@"image/jpeg"];
         }
     }
          success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         
         NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
         NSLog(@"完成 %@", result);
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"错误 %@", error.localizedDescription);
     }];
   
    
 }
转载出处 :http://blog.csdn.net/qq_31249697/article/details/50696784
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值