iOS實現壓縮圖片上傳功能_IOS

來源:互聯網
上載者:User

本文執行個體為大家分享了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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.