上传图片流到服务器(AFN方法) (多张图片)(图片流)
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);
}];
}