iOS图片压缩上传

本文实例为大家分享了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;

  }];

}

 

时间: 2024-10-29 19:12:10

iOS图片压缩上传的相关文章

IOS图片压缩上传服务器终极解决方案

我最终才去的方案如下: /** * 动态发布图片压缩 * * @param source_image 原图image * @param maxSize 限定的图片大小 * * @return 返回处理后的图片 */ - (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize; 先调整分辨率,分辨率可以自己设定一个值,大于的就缩小到这分辨率,小余的就保持原本分辨率.然后在根据最终大小来设置压

Html5+asp.net mvc 图片压缩上传

在做图片上传时,大图片如果没有压缩直接上传时间会非常长,因为有的图片太大,传到服务器上再压缩太慢了,而且损耗流量. 思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法得到base64字符串来实现压缩. 废话不多少不多说直接看代码: 本次测试使用了 zepto.min.js 插件,更新版本的下载请点击这里 主要js代码: //图片压缩处理 ; (function () { /** * 加载的时候进行抽样检测 * 在iOS中,大于2M的图片会抽样渲染 */ func

纯原生js移动端图片压缩上传插件

前段时间,同事又来咨询一个问题了,说手机端动不动拍照就好几M高清大图,上传服务器太慢,问问我有没有可以压缩图片并上传的js插件,当然手头上没有,别慌,我去网上搜一搜. 结果呢,呵呵...诶~又全是基于jquery.zepto的(这句话似乎吐槽次数太多了...),然后我也就不吐槽了, 然后当然是自己做了,先上图: 纯原生js的移动端图片压缩上传插件,不依赖任何库 用法 在html页面中引入input标签,通过自定义属性data-LUploader绑定点击触发的标签id,写法如下: <div cla

android图片压缩上传系列-service篇

本篇文章是继续上篇android图片压缩上传系列-基础篇文章的续篇.主要目的是:通过Service来执行图片压缩任务来讨论如何使用Service,如何处理任务量大的并发问题. 了解下Service 大家都知道如果有费时任务,这时需要将任务放到后台线程中执行,如果对操作的结果需要通过ui展示还需要在任务完成后通知前台更新.当然对于这种情况,大家也可以在Activity中启动线程,在线程中通过Handler和sendMessage来通知Activity并执行更新ui的操作,但是更好的方法是将这些操作

基于H5+ API手机相册图片压缩上传

// 母函数 function App(){} /** * 图片压缩,默认同比例压缩 * @param {Object} path * pc端传入的路径可以为相对路径,但是在移动端上必须传入的路径是照相图片储存的绝对路径 * @param {Object} obj * obj 对象 有 width, height, quality(0-1) * @param {Object} callback * 回调函数有一个参数,base64的字符串数据 */ App.prototype.dealImage

springMVC多图片压缩上传的实现

前台代码: <form id="userForm" method="post" enctype="multipart/form-data"> <div> <label class="my_input_title">图片1:</label> <input type="file" name="file" class=""

【方法】如何实现图片压缩上传

[参考文章] 前端实现图片压缩上传功能 H5实现图片先压缩再上传 原文地址:https://www.cnblogs.com/wannananana/p/12050442.html

前端图片压缩上传(纯js的质量压缩,非大小压缩)

此demo为大于1M对图片进行压缩上传 若小于1M则原图上传,可以根据自己实际需求更改. demo源码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>XMLHttpRequest上传文件</title> <script type="text/javascript"> /* 三个参数 file:一个是文件(

HTML5实现图片压缩上传功能

上篇文章中提到移动端上传图片,我们知道现在流量还是挺贵的,手机的像素是越来越高,拍个照动不动就是好几M,伤不起.虽然客户端可以轻轻松松实现图片压缩再上传,但是我们的应用还可能在浏览器里面打开,怎么办呢,图片压缩.受以前PC上的开发思维影响,尼玛js哪有权限去操作文件,哪有资格压缩图片啊,搞不了,你们客户端去整吧.只能说自己还是有些井底之蛙了.在HTML5的影响下,前端能干的事情越来越多了,开发的功能逼格也越来越高了,H5万岁!前端的魅力也在这,过去不可能的并不意味现在.以后不可能,努力吧,骚年!