服务器之家

服务器之家 > 正文

iOS实现压缩图片上传功能

时间:2021-01-18 13:22     来源/作者:风吹屁股微微凉

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#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程序设计有所帮助。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部