MyOrderVC.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. //
  2. // MyOrderVC.m
  3. // jiaPei
  4. //
  5. // Created by apple on 16/5/27.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "MyOrderVC.h"
  9. #import "STSegView.h"
  10. #import "DateView.h"
  11. #import "OrderCell.h"
  12. @interface MyOrderVC ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
  13. {
  14. STSegView *seg;
  15. UIScrollView *scroll;
  16. UITableView *mainTableView;
  17. UIBarButtonItem *item;
  18. UIView *backView;
  19. UIButton *beginBtn;
  20. UIButton *endBtn;
  21. HolderView *holder;
  22. NSMutableArray *dataArray;;
  23. NSString *beginDate;
  24. NSString *endDate;
  25. NSInteger currentPage;
  26. NSString *orderState;
  27. //生成收款二维码
  28. UIView *scanView;
  29. NSTimer *timer;
  30. NSString *payOrderId;
  31. NSString *studentId;
  32. NSString *studentOutId;
  33. NSString *reserveId;
  34. BOOL isAfterDelay;
  35. NSInteger timeI;
  36. }
  37. @end
  38. @implementation MyOrderVC
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. [self myInit];
  42. [self getMyOrderInfo];
  43. }
  44. -(void)viewWillDisappear:(BOOL)animated
  45. {
  46. [super viewWillDisappear:animated];
  47. [LoadingView removeHUD];
  48. }
  49. -(void)myInit
  50. {
  51. currentPage = 1;
  52. orderState = @"0";
  53. beginDate = @"";
  54. endDate = @"";
  55. dataArray = [NSMutableArray array];
  56. item = [[UIBarButtonItem alloc] initWithTitle:@"查找" style:UIBarButtonItemStyleDone target:self action:@selector(clickToSearch)];
  57. [item setTintColor:defGreen];
  58. [self.navigationItem setRightBarButtonItem:item];
  59. CGFloat x,y,w,h;
  60. x = 0;
  61. y = kNavOffSet;
  62. w = kSize.width;
  63. h = 50;
  64. seg = [[STSegView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  65. //0 进行中 2待支付 1 待评价/投诉 3 交易完成 4 投诉中 5 待结算
  66. [seg setTitles:@[@"计时中",@"待支付",@"待评价",@"已完成",@"已投诉",@"待结算"]];
  67. seg.selectedIndex = 0;
  68. [self.view addSubview:seg];
  69. y += h;
  70. h = kSize.height - y;
  71. mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, w, h) style:UITableViewStyleGrouped];
  72. mainTableView.delegate = self;
  73. mainTableView.dataSource = self;
  74. mainTableView.rowHeight = 115;
  75. [self.view addSubview:mainTableView];
  76. [seg click:^(NSString* sInd) {
  77. currentPage = 1;
  78. beginDate = @"";
  79. endDate = @"";
  80. orderState = sInd;
  81. [self getMyOrderInfo];
  82. }];
  83. holder = [[HolderView alloc] initWithFrame:mainTableView.frame];
  84. [holder freshBlock:^{
  85. currentPage = 1;
  86. [self getMyOrderInfo];
  87. }];
  88. [self.view addSubview:holder];
  89. }
  90. -(void)clickToSearch
  91. {
  92. item.enabled = NO;
  93. backView = [[UIView alloc] initWithFrame:kFrame];
  94. backView.backgroundColor = [UIColor colorWithWhite:.1 alpha:.5];
  95. [self.view addSubview:backView];
  96. CGFloat x, y, w, h, bd;
  97. x = 20;
  98. y = (kSize.height - 250)/2.0;
  99. w = kSize.width - 40;
  100. h = 250;
  101. UIView *searchView = [[UIView alloc] setxywh];
  102. searchView.backgroundColor = backGroundColor;
  103. searchView.layer.masksToBounds = YES;
  104. searchView.layer.cornerRadius = 5;
  105. [backView addSubview:searchView];
  106. x = y = bd = 15;
  107. w = searchView.width - 30;
  108. h = 50;
  109. NSArray *imgArray = @[@"beginDate",@"endDate"];
  110. NSString *beginDateStr = @"考试通过开始时间";
  111. NSString *endDateStr = @"考试通过结束时间";
  112. if (beginDate.length > 0) {
  113. beginDateStr = beginDate;
  114. }
  115. if (endDate.length > 0) {
  116. endDateStr = endDate;
  117. }
  118. NSArray *titles = @[beginDateStr,endDateStr];
  119. NSMutableArray *btns = [NSMutableArray arrayWithCapacity:3];
  120. for (int i = 0; i < 2; i ++) {
  121. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x + bd, 2*y + i*(h + bd), 20, 20)];
  122. imageView.image = [[UIImage imageNamed:imgArray[i]] tint:defGreen];
  123. [searchView addSubview:imageView];
  124. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y + i*(h + bd) , w, h)];
  125. [button setTitle:titles[i] textColor:contentTextColor font:16 fotState:UIControlStateNormal];
  126. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  127. button.titleEdgeInsets = UIEdgeInsetsMake(0, 3*bd, 0, 0);
  128. [button borderColor:lineColor width:2 cornorRadios:5];
  129. [button target:self Tag:i];
  130. [searchView addSubview:button];
  131. [btns addObject:button];
  132. }
  133. beginBtn = btns[0];
  134. endBtn = btns[1];
  135. y += 2*(h + bd);
  136. w = (w - 2*bd)/2.0;
  137. NSArray *titleArrays = @[@"取消",@"查询"];
  138. for (int i = 0; i < 2; i ++) {
  139. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x + i*(w + 2*bd), y , w, h)];
  140. [button setTitle:titleArrays[i] textColor:defGreen font:16 fotState:UIControlStateNormal];
  141. [button borderColor:lineColor width:2 cornorRadios:5];
  142. [button target:self Tag:i + 2];
  143. [searchView addSubview:button];
  144. }
  145. }
  146. -(void)btnClick:(UIButton *)sender
  147. {
  148. switch (sender.tag) {
  149. case 0:
  150. {
  151. DateView *dateV = [[DateView alloc] init];
  152. [dateV setStyle:0];
  153. [dateV showWithComplete:^(NSString * result) {
  154. beginDate = result;
  155. [beginBtn setTitle:beginDate forState:UIControlStateNormal];
  156. }];
  157. }
  158. break;
  159. case 1:
  160. {
  161. DateView *dateV = [[DateView alloc] init];
  162. [dateV setStyle:0];
  163. [dateV showWithComplete:^(NSString * result) {
  164. endDate = result;
  165. [endBtn setTitle:endDate forState:UIControlStateNormal];
  166. }];
  167. }
  168. break;
  169. case 2:
  170. {
  171. if (backView) {
  172. [backView removeFromSuperview];
  173. }
  174. item.enabled = YES;
  175. }
  176. break;
  177. case 3:
  178. {
  179. if (backView) {
  180. [backView removeFromSuperview];
  181. }
  182. item.enabled = YES;
  183. currentPage = 1;
  184. [self getMyOrderInfo];
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  192. {
  193. if (alertView.tag == 1) {
  194. if (buttonIndex != alertView.cancelButtonIndex) {
  195. [self orderPayWithType:buttonIndex];
  196. }
  197. }
  198. }
  199. #pragma mark tableView
  200. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  201. {
  202. return dataArray.count;
  203. }
  204. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  205. {
  206. return 1;
  207. }
  208. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  209. {
  210. OrderCell *cell = [OrderCell cellForTabelView:tableView];
  211. cell.skipVC = self;
  212. cell.type = orderState;
  213. [cell setDataDic:dataArray[indexPath.section]];
  214. [cell payClickToBlock:^(NSDictionary *dic) {
  215. payOrderId = dic[@"RO_ID"];
  216. studentId = dic[@"RO_USER_ID"];
  217. studentOutId = dic[@"OUTID"];
  218. reserveId = dic[@"RO_RESERVE_ID"];
  219. //弹出选择框 微信 QQ
  220. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请选择支付方式" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"支付宝支付",@"微信支付",@"本地支付", nil];
  221. alert.tag = 1;
  222. [alert show];
  223. }];
  224. return cell;
  225. }
  226. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  227. {
  228. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 15)];
  229. view.backgroundColor = lineColor;
  230. return view;
  231. }
  232. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  233. {
  234. return 15;
  235. }
  236. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  237. {
  238. UIView *view = [UIView new];
  239. return view;
  240. }
  241. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  242. {
  243. return .1;
  244. }
  245. -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  246. {
  247. CGPoint off = scrollView.contentOffset;
  248. if (scrollView.contentSize.height - off.y - scrollView.frame.size.height < 1) {
  249. [scrollView setContentOffset:CGPointMake(off.x, off.y - 10) animated:YES];
  250. [self getMyOrderInfo];
  251. }
  252. }
  253. #pragma mark 数据请求
  254. -(void)getMyOrderInfo
  255. {
  256. [LoadingView showHUD];
  257. if (![Util connectedToNetWork]) {
  258. return;
  259. }
  260. NSMutableArray *arr=[NSMutableArray array];
  261. [arr addPro:@"type" Value:@"2"];
  262. [arr addPro:@"userId" Value:defUser.userDict[@"id"]];
  263. [arr addPro:@"startTime" Value:beginDate];
  264. [arr addPro:@"endTime" Value:endDate];
  265. [arr addPro:@"status" Value:orderState];
  266. [arr addPro:@"isPage" Value:@"1"];
  267. [arr addPro:@"pageSize" Value:@"20"];
  268. [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%d",(int)currentPage]];
  269. NSString *method = @"getMyReserveOrders";
  270. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  271. RemoveHUD();
  272. //执行同步并取得结果
  273. if (currentPage == 1) {
  274. holder.hidden = NO;
  275. }
  276. //NSLog(@"我的订单-->%@-->%@",arr,root);
  277. if (!root) {
  278. ShowMsgFailed();
  279. return;
  280. }
  281. if ([root[@"code"] isEqualToString:@"1"]) {
  282. ShowMsg(root[@"body"]);
  283. return;
  284. }
  285. NSArray *array = root[@"body"];
  286. if ([array count] < 1) {
  287. ShowMsg(@"已加载全部");
  288. return;
  289. }
  290. if (currentPage == 1) {
  291. [dataArray removeAllObjects];
  292. }
  293. [dataArray addObjectsFromArray:array];
  294. currentPage += 1;
  295. holder.hidden = YES;
  296. [mainTableView reloadData];
  297. }];
  298. }
  299. //- (void)payReserveOrder {
  300. // [LoadingView showHUD];
  301. // if (![Util connectedToNetWork]) {
  302. // showMsgUnconnect();
  303. // return;
  304. // }
  305. // NSMutableArray *arr=[NSMutableArray array];
  306. //
  307. // [arr addPro:@"userId" Value:studentId];
  308. // [arr addPro:@"coachId" Value:defUser.userDict[@"id"]];
  309. // [arr addPro:@"orderId" Value:payOrderId];
  310. //
  311. // NSString *method = @"payReserveOrder";
  312. //
  313. // [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  314. // if (!root) {
  315. // RemoveHUD();
  316. // ShowMsg(@"操作失败,请重试!");
  317. // return;
  318. // }
  319. // if ([root[@"code"] isEqualToString:@"1"]) {
  320. // RemoveHUD();
  321. // ShowMsg(root[@"body"]);
  322. // return;
  323. // }
  324. // [self addStudentTrainOrder];
  325. // }];
  326. //}
  327. /// 生成建行支付订单
  328. - (void)addStudentTrainOrder {
  329. [LoadingView showHUD];
  330. if (![Util connectedToNetWork]) {
  331. showMsgUnconnect();
  332. return;
  333. }
  334. NSMutableArray *arr=[NSMutableArray array];
  335. [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]];
  336. [arr addPro:@"orderId" Value:payOrderId];
  337. NSString *method = @"addStudentTrainOrder";
  338. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  339. if (!root) {
  340. RemoveHUD();
  341. ShowMsg(@"操作失败,请重试!");
  342. return;
  343. }
  344. if ([root[@"code"] isEqualToString:@"1"]) {
  345. RemoveHUD();
  346. ShowMsg(root[@"body"]);
  347. return;
  348. }
  349. [self getReserveSweepQrcode];
  350. }];
  351. }
  352. /// 获取付款码
  353. - (void)getReserveSweepQrcode {
  354. NSMutableArray *arr=[NSMutableArray array];
  355. [arr addPro:@"stuOutId" Value:studentOutId];
  356. [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]];
  357. [arr addPro:@"reserveId" Value:reserveId];
  358. [arr addPro:@"orderId" Value:payOrderId];
  359. NSString *method = @"getReserveSweepQrcode";
  360. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  361. RemoveHUD();
  362. if (!root) {
  363. ShowMsg(@"操作失败,请重试!");
  364. return;
  365. }
  366. if ([root[@"code"] isEqualToString:@"1"] || [root[@"code"] isEqualToString:@"-1"]) {
  367. ShowMsg(root[@"body"]);
  368. return;
  369. }
  370. [self makeScanViewWithString:root[@"body"][@"qrCode"] withType:0];
  371. // [self getMyOrderInfo];
  372. }];
  373. }
  374. - (void)getReserveOrderStatus {
  375. NSMutableArray *arr = [NSMutableArray array];
  376. [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]];
  377. [arr addPro:@"orderId" Value:payOrderId];
  378. NSString *method = @"getReserveOrderStatus";
  379. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  380. if (!root) {
  381. ShowMsg(@"操作失败,请重试!");
  382. return;
  383. }
  384. if ([root[@"code"] isEqualToString:@"1"] || [root[@"code"] isEqualToString:@"-1"]) {
  385. ShowMsg(root[@"body"]);
  386. return;
  387. }
  388. }];
  389. }
  390. /** 订单支付
  391. @param type 1为QQ 2为微信 3本地
  392. */
  393. - (void)orderPayWithType:(NSInteger)type
  394. {
  395. if (type == 3) {
  396. UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"本地支付" message:@"是否本地支付完毕?" preferredStyle:UIAlertControllerStyleAlert];
  397. [alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:nil]];
  398. [alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  399. [self addStudentTrainOrder];
  400. }]];
  401. [self presentViewController:alert animated:true completion:nil];
  402. return;
  403. }
  404. [LoadingView showHUD];
  405. if (![Util connectedToNetWork]) {
  406. return;
  407. }
  408. NSMutableArray *arr=[NSMutableArray array];
  409. [arr addPro:@"id" Value:payOrderId];
  410. NSString *method = @"aliSweepPay";
  411. if (type == 2) {
  412. method = @"wechatSweepPay";
  413. }
  414. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  415. RemoveHUD();
  416. if (!root) {
  417. ShowMsg(@"操作失败,请重试!");
  418. return;
  419. }
  420. if ([root[@"code"] isEqualToString:@"1"]) {
  421. ShowMsg(root[@"body"]);
  422. return;
  423. }
  424. if ([root[@"body"] length] < 1) {
  425. ShowMsg(@"数据错误,请重试!");
  426. return;
  427. }
  428. [self makeScanViewWithString:root[@"body"] withType:type];
  429. }];
  430. }
  431. - (void)getOrderPayStatus
  432. {
  433. if (![Util connectedToNetWork]) {
  434. showMsgUnconnect();
  435. return;
  436. }
  437. NSString* method = [NSString stringWithFormat:@"getOrderPayStatus-%@",payOrderId];
  438. [jiaPeiManager requestGetAnythingWithURL:method completion:^(NSDictionary *dict) {
  439. if (!dict) {
  440. return;
  441. }
  442. if ([dict[@"code"] isEqualToString:@"1"] || [dict[@"code"] isEqualToString:@"3"]) {
  443. ShowMsg(dict[@"body"]);
  444. [self removeScanView];
  445. return;
  446. }
  447. if ([dict[@"code"] isEqualToString:@"0"]) {
  448. //交易完成
  449. ShowMsg(@"支付成功!");
  450. [self removeScanView];
  451. currentPage = 1;
  452. [self getMyOrderInfo];
  453. return;
  454. }
  455. }];
  456. }
  457. #pragma mark 生成二维码
  458. -(void)makeScanViewWithString:(NSString *)scanString withType:(NSInteger)type
  459. {
  460. [self closeTimer];
  461. CGFloat x,y,w,h;
  462. x = 20;
  463. w = kSize.width - 40;
  464. h = (w - 250)/2.0 + 300;
  465. y = (kSize.height - h)/2.0 - 30;
  466. scanView = [[UIView alloc] setxywh];
  467. scanView.backgroundColor = [UIColor whiteColor];
  468. [scanView borderColor:lineColor width:1 cornorRadios:5];
  469. [self.view addSubview:scanView];
  470. x = 0;
  471. y = 10;
  472. h = 30;
  473. UILabel *label = [[UILabel alloc] setxywh];
  474. [label setText:@"请扫下图二维码进行付费" Font:Font16 TextColor:[UIColor orangeColor] Alignment:NSTextAlignmentCenter];
  475. [scanView addSubview:label];
  476. UIImageView *cancelImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cancel"]];
  477. cancelImg.frame = CGRectMake(w - 25, 5, 20, 20);
  478. [scanView addSubview:cancelImg];
  479. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  480. btn.frame = CGRectMake(w - 50, 0, 50, 50);
  481. btn.backgroundColor = [UIColor clearColor];
  482. [btn addTarget:self action:@selector(removeScanViewAndIsCanTime) forControlEvents:UIControlEventTouchUpInside];
  483. [scanView addSubview:btn];
  484. x = (w - 250)/2.0;
  485. y += h + 10;
  486. w = h = 250;
  487. UIImageView *scanImgView = [[UIImageView alloc] setxywh];
  488. [scanView addSubview:scanImgView];
  489. //二维码
  490. UIImage *qrcode = [self createNonInterpolatedUIImageFormCIImage:[self createQRForString:scanString] withSize:250.0f];
  491. if (type == 1) {
  492. qrcode = [self addIconToQRCodeImage:qrcode withIcon:[UIImage imageNamed:@"AliPay.png"] withIconSize:CGSizeMake(40, 40)];
  493. }else if (type == 2){
  494. qrcode = [self addIconToQRCodeImage:qrcode withIcon:[UIImage imageNamed:@"WechatPay.png"] withIconSize:CGSizeMake(40, 40)];
  495. }
  496. scanImgView.image = qrcode;
  497. timeI = 0;
  498. timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
  499. }
  500. - (void)timeRun {
  501. timeI ++;
  502. if (timeI%5 == 0) {
  503. [self getOrderPayStatus];
  504. if (timeI == 1000) {
  505. timeI = 0;
  506. }
  507. }
  508. }
  509. - (void)removeScanViewAndIsCanTime {
  510. //不要立即关闭定时器 做30秒的延迟
  511. if (scanView) {
  512. [scanView removeFromSuperview];
  513. scanView = nil;
  514. }
  515. isAfterDelay = YES;
  516. [self performSelector:@selector(closeTimer) withObject:nil afterDelay:30];
  517. }
  518. - (void)closeTimer {
  519. if (isAfterDelay == YES) {
  520. if (timer) {
  521. [timer invalidate];
  522. timer = nil;
  523. }
  524. isAfterDelay = NO;
  525. }
  526. }
  527. - (void)removeScanView {
  528. if (timer) {
  529. [timer invalidate];
  530. timer = nil;
  531. }
  532. if (scanView) {
  533. [scanView removeFromSuperview];
  534. scanView = nil;
  535. }
  536. }
  537. - (CIImage *)createQRForString:(NSString *)qrString {
  538. NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
  539. // 创建filter
  540. CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  541. // 设置内容和纠错级别
  542. [qrFilter setValue:stringData forKey:@"inputMessage"];
  543. [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
  544. // 返回CIImage
  545. return qrFilter.outputImage;
  546. }
  547. - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
  548. CGRect extent = CGRectIntegral(image.extent);
  549. CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
  550. // 创建bitmap;
  551. size_t width = CGRectGetWidth(extent) * scale;
  552. size_t height = CGRectGetHeight(extent) * scale;
  553. CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
  554. CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
  555. CIContext *context = [CIContext contextWithOptions:nil];
  556. CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
  557. CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
  558. CGContextScaleCTM(bitmapRef, scale, scale);
  559. CGContextDrawImage(bitmapRef, extent, bitmapImage);
  560. // 保存bitmap到图片
  561. CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
  562. CGColorSpaceRelease(cs);
  563. CGContextRelease(bitmapRef);
  564. CGImageRelease(bitmapImage);
  565. UIImage * newImage = (UIImage *)[UIImage imageWithCGImage:scaledImage];
  566. CGImageRelease(scaledImage);
  567. return newImage;
  568. }
  569. -(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize {
  570. UIGraphicsBeginImageContext(image.size);
  571. //通过两张图片进行位置和大小的绘制,实现两张图片的合并;其实此原理做法也可以用于多张图片的合并
  572. CGFloat widthOfImage = image.size.width;
  573. CGFloat heightOfImage = image.size.height;
  574. CGFloat widthOfIcon = iconSize.width;
  575. CGFloat heightOfIcon = iconSize.height;
  576. [image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)];
  577. [icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2,
  578. widthOfIcon, heightOfIcon)];
  579. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  580. UIGraphicsEndImageContext();
  581. return img;
  582. }
  583. - (void)didReceiveMemoryWarning {
  584. [super didReceiveMemoryWarning];
  585. }
  586. @end