StitchingImage.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // StitchImage.m
  3. // StitchingImage
  4. //
  5. // Created by Jin on 10/9/15.
  6. // Copyright © 2015 Jin. All rights reserved.
  7. //
  8. #import "StitchingImage.h"
  9. const CGFloat marginSpaceRatio = 27.0;
  10. @interface StitchingImage ()
  11. {
  12. CGFloat _cellImageViewchrSideLength;
  13. CGFloat _margin;
  14. }
  15. @end
  16. @implementation StitchingImage
  17. - (UIImageView *)stitchingOnImageView:(UIImageView *)canvasView withImageViews:(NSArray *)imageViews {
  18. _margin = canvasView.frame.size.width / marginSpaceRatio;
  19. return [self stitchingOnImageView:canvasView withImageViews:imageViews marginValue:_margin];
  20. }
  21. - (UIImageView *)stitchingOnImageView:(UIImageView *)canvasView withImageViews:(NSArray *)imageViews marginValue:(CGFloat)_marginValue {
  22. _margin = _marginValue;
  23. [self generateImageViewSideLengthWithCanvasView:canvasView byImageViewsCount:imageViews.count];
  24. if ([imageViews count] == 1)
  25. {
  26. UIImageView* imageView_1 = imageViews[0];
  27. imageView_1.frame = CGRectMake(0, 0, _cellImageViewchrSideLength, _cellImageViewchrSideLength);
  28. }
  29. else if ([imageViews count] == 2)
  30. {
  31. CGFloat row_1_origin_y = (canvasView.frame.size.height - _cellImageViewchrSideLength) / 2;
  32. imageViews = [self generatorMatrix:imageViews beginOriginY:row_1_origin_y];
  33. }
  34. else if ([imageViews count] == 3)
  35. {
  36. CGFloat row_1_origin_y = (canvasView.frame.size.height - _cellImageViewchrSideLength * 2) / 3;
  37. UIImageView* imageView_1 = imageViews[0];
  38. imageView_1.frame = CGRectMake((canvasView.frame.size.width - _cellImageViewchrSideLength) / 2, row_1_origin_y, _cellImageViewchrSideLength, _cellImageViewchrSideLength);
  39. imageViews = [self generatorMatrix:imageViews beginOriginY:row_1_origin_y + _cellImageViewchrSideLength + _margin];
  40. }
  41. else if ([imageViews count] == 4)
  42. {
  43. CGFloat row_1_origin_y = (canvasView.frame.size.height - _cellImageViewchrSideLength * 2) / 3;
  44. imageViews = [self generatorMatrix:imageViews beginOriginY:row_1_origin_y];
  45. }
  46. else if ([imageViews count] == 5)
  47. {
  48. CGFloat row_1_origin_y = (canvasView.frame.size.height - _cellImageViewchrSideLength * 2 - _margin) / 2;
  49. UIImageView* imageView_1 = imageViews[0];
  50. imageView_1.frame = CGRectMake((canvasView.frame.size.width - 2 * _cellImageViewchrSideLength - _margin) / 2, row_1_origin_y, _cellImageViewchrSideLength, _cellImageViewchrSideLength);
  51. UIImageView* imageView_2 = imageViews[1];
  52. imageView_2.frame = CGRectMake(imageView_1.frame.origin.x + imageView_1.frame.size.width + _margin, row_1_origin_y, _cellImageViewchrSideLength, _cellImageViewchrSideLength);
  53. imageViews = [self generatorMatrix:imageViews beginOriginY:row_1_origin_y + _cellImageViewchrSideLength + _margin];
  54. }
  55. else if ([imageViews count] == 6)
  56. {
  57. CGFloat row_1_origin_y = (canvasView.frame.size.height - _cellImageViewchrSideLength * 2 - _margin) / 2;
  58. imageViews = [self generatorMatrix:imageViews beginOriginY:row_1_origin_y];
  59. }
  60. else if ([imageViews count] == 7)
  61. {
  62. CGFloat row_1_origin_y = (canvasView.frame.size.height - _cellImageViewchrSideLength * 3) / 4;
  63. UIImageView* imageView_1 = imageViews[0];
  64. imageView_1.frame = CGRectMake((canvasView.frame.size.width - _cellImageViewchrSideLength) / 2, row_1_origin_y, _cellImageViewchrSideLength, _cellImageViewchrSideLength);
  65. imageViews = [self generatorMatrix:imageViews beginOriginY:row_1_origin_y + _cellImageViewchrSideLength + _margin];
  66. }
  67. else if ([imageViews count] == 8)
  68. {
  69. CGFloat row_1_origin_y = (canvasView.frame.size.height - _cellImageViewchrSideLength * 3) / 4;
  70. UIImageView* imageView_1 = imageViews[0];
  71. imageView_1.frame = CGRectMake((canvasView.frame.size.width - 2 * _cellImageViewchrSideLength - _margin) / 2, row_1_origin_y, _cellImageViewchrSideLength, _cellImageViewchrSideLength);
  72. UIImageView* imageView_2 = imageViews[1];
  73. imageView_2.frame = CGRectMake(imageView_1.frame.origin.x + imageView_1.frame.size.width + _margin, row_1_origin_y, _cellImageViewchrSideLength, _cellImageViewchrSideLength);
  74. imageViews = [self generatorMatrix:imageViews beginOriginY:row_1_origin_y + _cellImageViewchrSideLength + _margin];
  75. }
  76. else if ([imageViews count] == 9)
  77. {
  78. CGFloat row_1_origin_y = (canvasView.frame.size.height - _cellImageViewchrSideLength * 3) / 4;
  79. imageViews = [self generatorMatrix:imageViews beginOriginY:row_1_origin_y];
  80. }
  81. for (UIImageView *imageView in imageViews) {
  82. [canvasView addSubview:imageView];
  83. }
  84. return canvasView;
  85. }
  86. - (NSArray *)generatorMatrix:(NSArray *)imageViews beginOriginY:(CGFloat)beginOriginY {
  87. int count = (int)imageViews.count;
  88. int cellCount;
  89. int maxRow;
  90. int maxColumn;
  91. int ignoreCountOfBegining;
  92. if (count <= 4)
  93. {
  94. maxRow = 2;
  95. maxColumn = 2;
  96. ignoreCountOfBegining = count % 2;
  97. cellCount = 4;
  98. }
  99. else
  100. {
  101. maxRow = 3;
  102. maxColumn = 3;
  103. ignoreCountOfBegining = count % 3;
  104. cellCount = 9;
  105. }
  106. for (int i = 0; i < cellCount; i++) {
  107. if (i > imageViews.count - 1) break;
  108. if (i < ignoreCountOfBegining) continue;
  109. int row = floor((float)(i - ignoreCountOfBegining) / maxRow);
  110. int column = (i - ignoreCountOfBegining) % maxColumn;
  111. CGFloat origin_x = _margin + _cellImageViewchrSideLength * column + _margin * column;
  112. CGFloat origin_y = beginOriginY + _cellImageViewchrSideLength * row + _margin * row;
  113. UIImageView* imageView = imageViews[i];
  114. imageView.frame = CGRectMake(origin_x, origin_y, _cellImageViewchrSideLength, _cellImageViewchrSideLength);
  115. }
  116. return imageViews;
  117. }
  118. - (void)generateImageViewSideLengthWithCanvasView:(UIView *)canvasView byImageViewsCount:(NSInteger)count {
  119. CGFloat sideLength = 0.0f;
  120. if (count == 1) {
  121. sideLength = canvasView.frame.size.width;
  122. } else if (count >=2 && count <=4) {
  123. sideLength = (canvasView.frame.size.width - _margin * 3) / 2;
  124. } else {
  125. sideLength = (canvasView.frame.size.width - _margin * 4) / 3;
  126. }
  127. _cellImageViewchrSideLength = sideLength;
  128. }
  129. @end