ImageHelpers.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. File: ImageHelpers.m
  3. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  4. Inc. ("Apple") in consideration of your agreement to the following
  5. terms, and your use, installation, modification or redistribution of
  6. this Apple software constitutes acceptance of these terms. If you do
  7. not agree with these terms, please do not use, install, modify or
  8. redistribute this Apple software.
  9. In consideration of your agreement to abide by the following terms, and
  10. subject to these terms, Apple grants you a personal, non-exclusive
  11. license, under Apple's copyrights in this original Apple software (the
  12. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  13. Software, with or without modifications, in source and/or binary forms;
  14. provided that if you redistribute the Apple Software in its entirety and
  15. without modifications, you must retain this notice and the following
  16. text and disclaimers in all such redistributions of the Apple Software.
  17. Neither the name, trademarks, service marks or logos of Apple Inc. may
  18. be used to endorse or promote products derived from the Apple Software
  19. without specific prior written permission from Apple. Except as
  20. expressly stated in this notice, no other rights or licenses, express or
  21. implied, are granted by Apple herein, including but not limited to any
  22. patent rights that may be infringed by your derivative works or by other
  23. works in which the Apple Software may be incorporated.
  24. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  25. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  26. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  27. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  28. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  29. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  30. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  33. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  34. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  35. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  36. POSSIBILITY OF SUCH DAMAGE.
  37. Copyright (C) 2009 Apple Inc. All Rights Reserved.
  38. */
  39. #include "ImageHelpers.h"
  40. const CGBitmapInfo kDefaultCGBitmapInfo = (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
  41. const CGBitmapInfo kDefaultCGBitmapInfoNoAlpha = (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host);
  42. CGColorSpaceRef GetDeviceRGBColorSpace() {
  43. static CGColorSpaceRef deviceRGBSpace = NULL;
  44. if( deviceRGBSpace == NULL )
  45. deviceRGBSpace = CGColorSpaceCreateDeviceRGB();
  46. return deviceRGBSpace;
  47. }
  48. float GetScaleForProportionalResize( CGSize theSize, CGSize intoSize, bool onlyScaleDown, bool maximize )
  49. {
  50. float sx = theSize.width;
  51. float sy = theSize.height;
  52. float dx = intoSize.width;
  53. float dy = intoSize.height;
  54. float scale = 1;
  55. if( sx != 0 && sy != 0 )
  56. {
  57. dx = dx / sx;
  58. dy = dy / sy;
  59. // if maximize is true, take LARGER of the scales, else smaller
  60. if( maximize ) scale = (dx > dy) ? dx : dy;
  61. else scale = (dx < dy) ? dx : dy;
  62. if( scale > 1 && onlyScaleDown ) // reset scale
  63. scale = 1;
  64. }
  65. else
  66. {
  67. scale = 0;
  68. }
  69. return scale;
  70. }
  71. CGContextRef CreateCGBitmapContextForWidthAndHeight( unsigned int width, unsigned int height,
  72. CGColorSpaceRef optionalColorSpace, CGBitmapInfo optionalInfo )
  73. {
  74. CGColorSpaceRef colorSpace = (optionalColorSpace == NULL) ? GetDeviceRGBColorSpace() : optionalColorSpace;
  75. CGBitmapInfo alphaInfo = ( (int32_t)optionalInfo < 0 ) ? kDefaultCGBitmapInfo : optionalInfo;
  76. return CGBitmapContextCreate( NULL, width, height, 8, 0, colorSpace, alphaInfo );
  77. }
  78. CGImageRef CreateCGImageFromUIImageScaled( UIImage* image, float scaleFactor )
  79. {
  80. CGImageRef newImage = NULL;
  81. CGContextRef bmContext = NULL;
  82. BOOL mustTransform = YES;
  83. CGAffineTransform transform = CGAffineTransformIdentity;
  84. UIImageOrientation orientation = image.imageOrientation;
  85. CGImageRef srcCGImage = CGImageRetain( image.CGImage );
  86. size_t width = CGImageGetWidth(srcCGImage) * scaleFactor;
  87. size_t height = CGImageGetHeight(srcCGImage) * scaleFactor;
  88. // These Orientations are rotated 0 or 180 degrees, so they retain the width/height of the image
  89. if( (orientation == UIImageOrientationUp) || (orientation == UIImageOrientationDown) || (orientation == UIImageOrientationUpMirrored) || (orientation == UIImageOrientationDownMirrored) )
  90. {
  91. bmContext = CreateCGBitmapContextForWidthAndHeight( width, height, NULL, kDefaultCGBitmapInfo );
  92. }
  93. else // The other Orientations are rotated ±90 degrees, so they swap width & height.
  94. {
  95. bmContext = CreateCGBitmapContextForWidthAndHeight( height, width, NULL, kDefaultCGBitmapInfo );
  96. }
  97. //CGContextSetInterpolationQuality( bmContext, kCGInterpolationLow );
  98. CGContextSetBlendMode( bmContext, kCGBlendModeCopy ); // we just want to copy the data
  99. switch(orientation)
  100. {
  101. case UIImageOrientationDown: // 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees
  102. transform = CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height);
  103. break;
  104. case UIImageOrientationLeft: // 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees
  105. transform = CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0);
  106. break;
  107. case UIImageOrientationRight: // 0th row is on the right, and 0th column is the top - Rotate 90 degrees
  108. transform = CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width);
  109. break;
  110. case UIImageOrientationUpMirrored: // 0th row is at the top, and 0th column is on the right - Flip Horizontal
  111. transform = CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0);
  112. break;
  113. case UIImageOrientationDownMirrored: // 0th row is at the bottom, and 0th column is on the left - Flip Vertical
  114. transform = CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height);
  115. break;
  116. case UIImageOrientationLeftMirrored: // 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical
  117. transform = CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width);
  118. break;
  119. case UIImageOrientationRightMirrored: // 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical
  120. transform = CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0);
  121. break;
  122. default:
  123. mustTransform = NO;
  124. break;
  125. }
  126. if( mustTransform ) CGContextConcatCTM( bmContext, transform );
  127. CGContextDrawImage( bmContext, CGRectMake(0.0, 0.0, width, height), srcCGImage );
  128. CGImageRelease( srcCGImage );
  129. newImage = CGBitmapContextCreateImage( bmContext );
  130. CFRelease( bmContext );
  131. return newImage;
  132. }
  133. @implementation UIImage (scale)
  134. -(UIImage*) scaleToSize:(CGSize)toSize
  135. {
  136. UIImage *scaledImg = nil;
  137. float scale = GetScaleForProportionalResize( self.size, toSize, false, false );
  138. CGImageRef cgImage = CreateCGImageFromUIImageScaled( self, scale );
  139. if( cgImage )
  140. {
  141. scaledImg = [UIImage imageWithCGImage:cgImage]; // autoreleased
  142. CGImageRelease( cgImage );
  143. }
  144. return scaledImg;
  145. }
  146. @end