• 上传图片流到服务器(AFN方法) (多张图片)(图片流)


     

    上传图片流到服务器(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);

         }];

       

        

     }


  • 相关阅读:
    【原创】Apache ab结果参数详解
    【转载】QPS,用户平均等待时间,服务器平均请求处理时间
    【原创】Apache ab测试时出现:apr_socket_recv "connection reset by peer" 104
    【做题】Codeforces Round #429 (Div. 2) E. On the Bench——组合问题+dp
    oracle递归查询
    http1.0和1.1的区别
    here with you
    spring杂碎
    西海情歌
    //随机生成 10到20条数据 数据包含 用户名(5-10位的字母) 性别 年龄(1-100岁)
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/6860481.html
Copyright © 2020-2023  润新知