iOS realizes compressed image upload function

  • 2020-06-03 08:32:04
  • OfStack

This article shares iOS's compressed image uploading function for your reference. The specific content is as follows


#pragma mark -  Open the camera 
-(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 = @" A system exception ";
      }
      [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 = @" Uploaded successfully ";
      [AlertHelper shareAlertHelper].alertMessage = nil;
      [[AlertHelper shareAlertHelper] alertVcWithSureAction:^(id response) {
         
      }];
       
      if ([self.currentTapCell.photoDescribeLable.text isEqualToString:@" Upload diagnostic certificate "]) {
        [self.successUploadDic setValue:response[@"FILEID"] forKey:@"PROVEID"];
      }
      if ([self.currentTapCell.photoDescribeLable.text isEqualToString:@" Upload the first page of medical records "]){
        [self.successUploadDic setValue:response[@"FILEID"] forKey:@"CASEHOMEID"];
      }
       
       
       
      NSLog(@" Upload the result   %@", 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 -  Open the camera 
-(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 = @" A system exception ";
      }
      [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 = @" Uploaded successfully ";
      [AlertHelper shareAlertHelper].alertMessage = nil;
      [[AlertHelper shareAlertHelper] alertVcWithSureAction:^(id response) {
         
      }];
       
      if ([self.currentTapCell.photoDescribeLable.text isEqualToString:@" Upload diagnostic certificate "]) {
        [self.successUploadDic setValue:response[@"FILEID"] forKey:@"PROVEID"];
      }
      if ([self.currentTapCell.photoDescribeLable.text isEqualToString:@" Upload the first page of medical records "]){
        [self.successUploadDic setValue:response[@"FILEID"] forKey:@"CASEHOMEID"];
      }
       
       
       
      NSLog(@" Upload the result   %@", 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:^{
     
  }];
}
          
 
 
 
/** Upload a file */
-(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;
  }];
}

That's the end of this article, and I hope it will help you learn iOS programming.


Related articles: