zoukankan      html  css  js  c++  java
  • 上传图片流到服务器(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);

         }];

       

        

     }


  • 相关阅读:
    [Reinforcement Learning] Cross-entropy Method
    [Deep Learning] 正则化
    [Deep Learning] 常用的Active functions & Optimizers
    [Machine Learning] 浅谈LR算法的Cost Function
    [Deep Learning] 深度学习中消失的梯度
    [Machine Learning] logistic函数和softmax函数
    [Deep Learning] 神经网络基础
    [Machine Learning] Active Learning
    [Machine Learning & Algorithm]CAML机器学习系列2:深入浅出ML之Entropy-Based家族
    [Machine Learning & Algorithm]CAML机器学习系列1:深入浅出ML之Regression家族
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/6860481.html
Copyright © 2011-2022 走看看