PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/iPhone/GTMUIImage+Resize.m

http://macfuse.googlecode.com/
Objective C | 186 lines | 129 code | 26 blank | 31 comment | 14 complexity | 10e3f5052dc4dbfcb514e701571e5cf9 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  1. //
  2. // GTMUIImage+Resize.m
  3. //
  4. // Copyright 2009 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMUIImage+Resize.h"
  19. #import "GTMDefines.h"
  20. GTM_INLINE CGSize swapWidthAndHeight(CGSize size) {
  21. CGFloat tempWidth = size.width;
  22. size.width = size.height;
  23. size.height = tempWidth;
  24. return size;
  25. }
  26. @implementation UIImage (GTMUIImageResizeAdditions)
  27. - (UIImage *)gtm_imageByResizingToSize:(CGSize)targetSize
  28. preserveAspectRatio:(BOOL)preserveAspectRatio
  29. trimToFit:(BOOL)trimToFit {
  30. CGSize imageSize = [self size];
  31. if (imageSize.height < 1 || imageSize.width < 1) {
  32. return nil;
  33. }
  34. if (targetSize.height < 1 || targetSize.width < 1) {
  35. return nil;
  36. }
  37. CGFloat aspectRatio = imageSize.width / imageSize.height;
  38. CGFloat targetAspectRatio = targetSize.width / targetSize.height;
  39. CGRect projectTo = CGRectZero;
  40. if (preserveAspectRatio) {
  41. if (trimToFit) {
  42. // Scale and clip image so that the aspect ratio is preserved and the
  43. // target size is filled.
  44. if (targetAspectRatio < aspectRatio) {
  45. // clip the x-axis.
  46. projectTo.size.width = targetSize.height * aspectRatio;
  47. projectTo.size.height = targetSize.height;
  48. projectTo.origin.x = (targetSize.width - projectTo.size.width) / 2;
  49. projectTo.origin.y = 0;
  50. } else {
  51. // clip the y-axis.
  52. projectTo.size.width = targetSize.width;
  53. projectTo.size.height = targetSize.width / aspectRatio;
  54. projectTo.origin.x = 0;
  55. projectTo.origin.y = (targetSize.height - projectTo.size.height) / 2;
  56. }
  57. } else {
  58. // Scale image to ensure it fits inside the specified targetSize.
  59. if (targetAspectRatio < aspectRatio) {
  60. // target is less wide than the original.
  61. projectTo.size.width = targetSize.width;
  62. projectTo.size.height = projectTo.size.width / aspectRatio;
  63. targetSize = projectTo.size;
  64. } else {
  65. // target is wider than the original.
  66. projectTo.size.height = targetSize.height;
  67. projectTo.size.width = projectTo.size.height * aspectRatio;
  68. targetSize = projectTo.size;
  69. }
  70. } // if (clip)
  71. } else {
  72. // Don't preserve the aspect ratio.
  73. projectTo.size = targetSize;
  74. }
  75. projectTo = CGRectIntegral(projectTo);
  76. // There's no CGSizeIntegral, so we fake our own.
  77. CGRect integralRect = CGRectZero;
  78. integralRect.size = targetSize;
  79. targetSize = CGRectIntegral(integralRect).size;
  80. // Resize photo. Use UIImage drawing methods because they respect
  81. // UIImageOrientation as opposed to CGContextDrawImage().
  82. UIGraphicsBeginImageContext(targetSize);
  83. [self drawInRect:projectTo];
  84. UIImage* resizedPhoto = UIGraphicsGetImageFromCurrentImageContext();
  85. UIGraphicsEndImageContext();
  86. return resizedPhoto;
  87. }
  88. // Based on code by Trevor Harmon:
  89. // http://vocaro.com/trevor/blog/wp-content/uploads/2009/10/UIImage+Resize.h
  90. // http://vocaro.com/trevor/blog/wp-content/uploads/2009/10/UIImage+Resize.m
  91. - (UIImage *)gtm_imageByRotating:(UIImageOrientation)orientation {
  92. CGRect bounds = CGRectZero;
  93. CGRect rect = CGRectZero;
  94. CGAffineTransform transform = CGAffineTransformIdentity;
  95. bounds.size = [self size];
  96. rect.size = [self size];
  97. switch (orientation) {
  98. case UIImageOrientationUp:
  99. return [UIImage imageWithCGImage:[self CGImage]];
  100. case UIImageOrientationUpMirrored:
  101. transform = CGAffineTransformMakeTranslation(rect.size.width, 0.0);
  102. transform = CGAffineTransformScale(transform, -1.0, 1.0);
  103. break;
  104. case UIImageOrientationDown:
  105. transform = CGAffineTransformMakeTranslation(rect.size.width,
  106. rect.size.height);
  107. transform = CGAffineTransformRotate(transform, M_PI);
  108. break;
  109. case UIImageOrientationDownMirrored:
  110. transform = CGAffineTransformMakeTranslation(0.0, rect.size.height);
  111. transform = CGAffineTransformScale(transform, 1.0, -1.0);
  112. break;
  113. case UIImageOrientationLeft:
  114. bounds.size = swapWidthAndHeight(bounds.size);
  115. transform = CGAffineTransformMakeTranslation(0.0, rect.size.width);
  116. transform = CGAffineTransformRotate(transform, -M_PI_2);
  117. break;
  118. case UIImageOrientationLeftMirrored:
  119. bounds.size = swapWidthAndHeight(bounds.size);
  120. transform = CGAffineTransformMakeTranslation(rect.size.height,
  121. rect.size.width);
  122. transform = CGAffineTransformScale(transform, -1.0, 1.0);
  123. transform = CGAffineTransformRotate(transform, -M_PI_2);
  124. break;
  125. case UIImageOrientationRight:
  126. bounds.size = swapWidthAndHeight(bounds.size);
  127. transform = CGAffineTransformMakeTranslation(rect.size.height, 0.0);
  128. transform = CGAffineTransformRotate(transform, M_PI_2);
  129. break;
  130. case UIImageOrientationRightMirrored:
  131. bounds.size = swapWidthAndHeight(bounds.size);
  132. transform = CGAffineTransformMakeScale(-1.0, 1.0);
  133. transform = CGAffineTransformRotate(transform, M_PI_2);
  134. break;
  135. default:
  136. _GTMDevAssert(false, @"Invalid orientation %zd", orientation);
  137. return nil;
  138. }
  139. UIGraphicsBeginImageContext(bounds.size);
  140. CGContextRef context = UIGraphicsGetCurrentContext();
  141. switch (orientation) {
  142. case UIImageOrientationLeft:
  143. case UIImageOrientationLeftMirrored:
  144. case UIImageOrientationRight:
  145. case UIImageOrientationRightMirrored:
  146. CGContextScaleCTM(context, -1.0, 1.0);
  147. CGContextTranslateCTM(context, -rect.size.height, 0.0);
  148. break;
  149. default:
  150. CGContextScaleCTM(context, 1.0, -1.0);
  151. CGContextTranslateCTM(context, 0.0, -rect.size.height);
  152. break;
  153. }
  154. CGContextConcatCTM(context, transform);
  155. CGContextDrawImage(context, rect, [self CGImage]);
  156. UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
  157. UIGraphicsEndImageContext();
  158. return rotatedImage;
  159. }
  160. @end