欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现压缩图片上传功能

程序员文章站 2023-12-12 18:54:04
本文实例为大家分享了ios实现压缩图片上传功能,供大家参考,具体内容如下 #pragma mark - 打开相机 -(void)imagepickercontr...

本文实例为大家分享了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程序设计有所帮助。

上一篇:

下一篇: