欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS实现压缩图片上传功能

时间:2021-04-05 10:09:34|栏目:iOS代码|点击:

本文实例为大家分享了iOS实现压缩图片上传功能,供大家参考,具体内容如下

#pragma mark - 打开相机
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
  UIImage *image = info[UIImagePickerControllerOriginalImage];
  self.currentTapCell.photoManageImgView.image = image;
  NSIndexPath * indexPath = [self.baseTable indexPathForCell:self.currentTapCell];
  NSString * key = [NSString stringWithFormat:@"ineed%u%u", indexPath.row, indexPath.section];
    [[NetEngineHelper shareNetEngine] postUploadWithUrl:k_uploadPhoto_url image:image fileName:[NSString stringWithFormat:@"%@.png", key] fileType:@"PNG/JPEG/JPG" success:^(id response) {
    if ([response[@"STATUS"] intValue] != 0) {
      NSString * str = @"";
      if (response[@"ERRORDESC"]) {
        str = response[@"ERRORDESC"];
      }else{
        str = @"系统异常";
      }
      [AlertHelper shareAlertHelper].onVC = self;
      [AlertHelper shareAlertHelper].alertTitle = str;
      [AlertHelper shareAlertHelper].alertMessage = nil;
      [[AlertHelper shareAlertHelper] alertVcWithSureAction:^(id response) {
         
      }];
    }else{
      [AlertHelper shareAlertHelper].onVC = self;
      [AlertHelper shareAlertHelper].alertTitle = @"上传成功";
      [AlertHelper shareAlertHelper].alertMessage = nil;
      [[AlertHelper shareAlertHelper] alertVcWithSureAction:^(id response) {
         
      }];
       
      if ([self.currentTapCell.photoDescribeLable.text isEqualToString:@"上传诊断证明"]) {
        [self.successUploadDic setValue:response[@"FILEID"] forKey:@"PROVEID"];
      }
      if ([self.currentTapCell.photoDescribeLable.text isEqualToString:@"上传病历首页"]){
        [self.successUploadDic setValue:response[@"FILEID"] forKey:@"CASEHOMEID"];
      }
       
       
       
      NSLog(@"上传结果  %@", response);
    }
 
  } fail:^(NSError *error) {
    [AlertHelper shareAlertHelper].onVC = self;
    [AlertHelper shareAlertHelper].alertTitle = error.userInfo[@"NSLocalizedDescription"];
    [AlertHelper shareAlertHelper].alertMessage = nil;
    [[AlertHelper shareAlertHelper] alertVcWithSureAction:^(id response) {
       
    }];
 
  }];
   
   
  [self dismissViewControllerAnimated:YES completion:^{
     
  }];
}
          
 
 
#pragma mark - 打开相机
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
  UIImage *image = info[UIImagePickerControllerOriginalImage];
  self.currentTapCell.photoManageImgView.image = image;
  NSIndexPath * indexPath = [self.baseTable indexPathForCell:self.currentTapCell];
  NSString * key = [NSString stringWithFormat:@"ineed%u%u", indexPath.row, indexPath.section];
    [[NetEngineHelper shareNetEngine] postUploadWithUrl:k_uploadPhoto_url image:image fileName:[NSString stringWithFormat:@"%@.png", key] fileType:@"PNG/JPEG/JPG" success:^(id response) {
    if ([response[@"STATUS"] intValue] != 0) {
      NSString * str = @"";
      if (response[@"ERRORDESC"]) {
        str = response[@"ERRORDESC"];
      }else{
        str = @"系统异常";
      }
      [AlertHelper shareAlertHelper].onVC = self;
      [AlertHelper shareAlertHelper].alertTitle = str;
      [AlertHelper shareAlertHelper].alertMessage = nil;
      [[AlertHelper shareAlertHelper] alertVcWithSureAction:^(id response) {
         
      }];
    }else{
      [AlertHelper shareAlertHelper].onVC = self;
      [AlertHelper shareAlertHelper].alertTitle = @"上传成功";
      [AlertHelper shareAlertHelper].alertMessage = nil;
      [[AlertHelper shareAlertHelper] alertVcWithSureAction:^(id response) {
         
      }];
       
      if ([self.currentTapCell.photoDescribeLable.text isEqualToString:@"上传诊断证明"]) {
        [self.successUploadDic setValue:response[@"FILEID"] forKey:@"PROVEID"];
      }
      if ([self.currentTapCell.photoDescribeLable.text isEqualToString:@"上传病历首页"]){
        [self.successUploadDic setValue:response[@"FILEID"] forKey:@"CASEHOMEID"];
      }
       
       
       
      NSLog(@"上传结果  %@", response);
    }
 
  } fail:^(NSError *error) {
    [AlertHelper shareAlertHelper].onVC = self;
    [AlertHelper shareAlertHelper].alertTitle = error.userInfo[@"NSLocalizedDescription"];
    [AlertHelper shareAlertHelper].alertMessage = nil;
    [[AlertHelper shareAlertHelper] alertVcWithSureAction:^(id response) {
       
    }];
 
  }];
   
   
  [self dismissViewControllerAnimated:YES completion:^{
     
  }];
}
          
 
 
 
/**上传文件*/
-(void)postUploadWithUrl:(NSString *)urlStr image:(UIImage *)image fileName:(NSString *)fileName fileType:(NSString *)fileTye success:(Success)success fail:(Faile)fail{
   
  NSString * resultStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  self.success = success;
  self.failer = fail;
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/plain" ,nil];
  [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  manager.responseSerializer=[AFJSONResponseSerializer serializer];
  manager.requestSerializer = [AFHTTPRequestSerializer serializer];
  [manager.securityPolicy setAllowInvalidCertificates:YES];
   
  UIApplication *application = [UIApplication sharedApplication];
  application.networkActivityIndicatorVisible = YES;
  [manager POST:resultStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
     
    NSData * imgData = UIImageJPEGRepresentation(image, 0.02);
    [formData appendPartWithFileData:imgData name:@"FILENAME" fileName:fileName mimeType:fileTye];
     
     
  } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    success(responseObject);
    application.networkActivityIndicatorVisible = NO;
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    fail(error);
    application.networkActivityIndicatorVisible = NO;
  }];
}

以上就是本文的全部内容,希望对大家学习iOS程序设计有所帮助。

上一篇:IOS开发OC代码中创建Swift编写的视图控制器

栏    目:iOS代码

下一篇:iOS使用pageViewController实现多视图滑动切换

本文标题:iOS实现压缩图片上传功能

本文地址:http://www.codeinn.net/misctech/94820.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有