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);

         }];

       

        

     }


  • 相关阅读:
    scrapy爬虫框架
    运用GRASP原则来做uml交互类图-------pos机实例
    csv文件读取
    文件编码解读
    smtp模块使用
    csv模块简单使用
    用Beautifulsoup 来爬取贴吧图片
    批处理命令——set
    bat批处理教程之for的/f参数
    docker 下 alpine 镜像设置时区的有效办法
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/6860481.html
Copyright © 2011-2022 走看看