ImageResize.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. Erica Sadun, http://ericasadun.com
  3. iPhone Developer's Cookbook, 3.0 Edition
  4. BSD License, Use at your own risk
  5. */
  6. #import "ImageResize.h"
  7. #import <QuartzCore/QuartzCore.h>
  8. #if SUPPPORTS_UNDOCUMENTED_APIS
  9. @interface UIImage (privateAPISForOrientation)
  10. - (id)initWithCGImage:(struct CGImage *)fp8 imageOrientation:(int)fp12;
  11. @end
  12. #endif
  13. @implementation ImageResize
  14. #pragma mark Create Image
  15. // Screen shot the view
  16. + (UIImage *) imageFromView: (UIView *) theView
  17. {
  18. UIGraphicsBeginImageContext(theView.frame.size);
  19. CGContextRef context = UIGraphicsGetCurrentContext();
  20. [theView.layer renderInContext:context];
  21. UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  22. UIGraphicsEndImageContext();
  23. return theImage;
  24. }
  25. #pragma mark Base Image Utility
  26. + (CGSize) fitSize: (CGSize)thisSize inSize: (CGSize) aSize
  27. {
  28. CGFloat scale;
  29. CGSize newsize = thisSize;
  30. if (newsize.height && (newsize.height > aSize.height))
  31. {
  32. scale = aSize.height / newsize.height;
  33. newsize.width *= scale;
  34. newsize.height *= scale;
  35. }
  36. if (newsize.width && (newsize.width >= aSize.width))
  37. {
  38. scale = aSize.width / newsize.width;
  39. newsize.width *= scale;
  40. newsize.height *= scale;
  41. }
  42. return newsize;
  43. }
  44. #define MIRRORED ((image.imageOrientation == UIImageOrientationUpMirrored) || (image.imageOrientation == UIImageOrientationLeftMirrored) || (image.imageOrientation == UIImageOrientationRightMirrored) || (image.imageOrientation == UIImageOrientationDownMirrored))
  45. #define ROTATED90 ((image.imageOrientation == UIImageOrientationLeft) || (image.imageOrientation == UIImageOrientationLeftMirrored) || (image.imageOrientation == UIImageOrientationRight) || (image.imageOrientation == UIImageOrientationRightMirrored))
  46. + (UIImage *) doUnrotateImage: (UIImage *) image
  47. {
  48. CGSize size = image.size;
  49. if (ROTATED90) size = CGSizeMake(image.size.height, image.size.width);
  50. UIGraphicsBeginImageContext(size);
  51. CGContextRef context = UIGraphicsGetCurrentContext();
  52. CGAffineTransform transform = CGAffineTransformIdentity;
  53. // Rotate as needed
  54. switch(image.imageOrientation)
  55. {
  56. case UIImageOrientationLeft:
  57. case UIImageOrientationRightMirrored:
  58. transform = CGAffineTransformRotate(transform, M_PI / 2.0f);
  59. transform = CGAffineTransformTranslate(transform, 0.0f, -size.width);
  60. size = CGSizeMake(size.height, size.width);
  61. CGContextConcatCTM(context, transform);
  62. break;
  63. case UIImageOrientationRight:
  64. case UIImageOrientationLeftMirrored:
  65. transform = CGAffineTransformRotate(transform, -M_PI / 2.0f);
  66. transform = CGAffineTransformTranslate(transform, -size.height, 0.0f);
  67. size = CGSizeMake(size.height, size.width);
  68. CGContextConcatCTM(context, transform);
  69. break;
  70. case UIImageOrientationDown:
  71. case UIImageOrientationDownMirrored:
  72. transform = CGAffineTransformRotate(transform, M_PI);
  73. transform = CGAffineTransformTranslate(transform, -size.width, -size.height);
  74. CGContextConcatCTM(context, transform);
  75. break;
  76. default:
  77. break;
  78. }
  79. if (MIRRORED)
  80. {
  81. // de-mirror
  82. transform = CGAffineTransformMakeTranslation(size.width, 0.0f);
  83. transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
  84. CGContextConcatCTM(context, transform);
  85. }
  86. // Draw the image into the transformed context and return the image
  87. [image drawAtPoint:CGPointMake(0.0f, 0.0f)];
  88. UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
  89. UIGraphicsEndImageContext();
  90. return newimg;
  91. }
  92. + (UIImage *) unrotateImage: (UIImage *) image
  93. {
  94. if (image.imageOrientation == UIImageOrientationUp) return image;
  95. return [ImageResize doUnrotateImage:image];
  96. }
  97. // Proportionately resize, completely fit in view, no cropping
  98. + (UIImage *) image: (UIImage *) image fitInSize: (CGSize) viewsize
  99. {
  100. // calculate the fitted size
  101. CGSize size = [ImageResize fitSize:image.size inSize:viewsize];
  102. UIGraphicsBeginImageContext(viewsize);
  103. float dwidth = (viewsize.width - size.width) / 2.0f;
  104. float dheight = (viewsize.height - size.height) / 2.0f;
  105. CGRect rect = CGRectMake(dwidth, dheight, size.width, size.height);
  106. [image drawInRect:rect];
  107. UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
  108. UIGraphicsEndImageContext();
  109. return newimg;
  110. }
  111. + (UIImage *) image: (UIImage *) image fitInView: (UIView *) view
  112. {
  113. return [self image:image fitInSize:view.frame.size];
  114. }
  115. // No resize, may crop
  116. + (UIImage *) image: (UIImage *) image centerInSize: (CGSize) viewsize
  117. {
  118. CGSize size = image.size;
  119. UIGraphicsBeginImageContext(viewsize);
  120. float dwidth = (viewsize.width - size.width) / 2.0f;
  121. float dheight = (viewsize.height - size.height) / 2.0f;
  122. CGRect rect = CGRectMake(dwidth, dheight, size.width, size.height);
  123. [image drawInRect:rect];
  124. UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
  125. UIGraphicsEndImageContext();
  126. return newimg;
  127. }
  128. + (UIImage *) image: (UIImage *) image centerInView: (UIView *) view
  129. {
  130. return [self image:image centerInSize:view.frame.size];
  131. }
  132. // Fill every view pixel with no black borders, resize and crop if needed
  133. + (UIImage *) image: (UIImage *) image fillSize: (CGSize) viewsize
  134. {
  135. CGSize size = image.size;
  136. CGFloat scalex = viewsize.width / size.width;
  137. CGFloat scaley = viewsize.height / size.height;
  138. CGFloat scale = MAX(scalex, scaley);
  139. UIGraphicsBeginImageContext(viewsize);
  140. CGFloat width = size.width * scale;
  141. CGFloat height = size.height * scale;
  142. float dwidth = ((viewsize.width - width) / 2.0f);
  143. float dheight = ((viewsize.height - height) / 2.0f);
  144. CGRect rect = CGRectMake(dwidth, dheight, size.width * scale, size.height * scale);
  145. [image drawInRect:rect];
  146. UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
  147. UIGraphicsEndImageContext();
  148. return newimg;
  149. }
  150. + (UIImage *) image: (UIImage *) image fillView: (UIView *) view
  151. {
  152. return [self image:image fillSize:view.frame.size];
  153. }
  154. #if SUPPPORTS_UNDOCUMENTED_APIS
  155. + (UIImage *) image: (UIImage *) image withOrientation: (UIImageOrientation) orientation
  156. {
  157. UIImage *newimg = [[UIImage alloc] initWithCGImage:[image CGImage] imageOrientation:orientation];
  158. return [newimg autorelease];
  159. }
  160. #endif
  161. + (UIImage *) getImageFromView:(UIView*) vi{
  162. UIGraphicsBeginImageContext(vi.frame.size);
  163. CGContextRef context = UIGraphicsGetCurrentContext();
  164. [vi.layer renderInContext:context];
  165. UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
  166. UIGraphicsEndImageContext();
  167. return image;
  168. }
  169. + (BOOL) saveImageFromView:(NSString*)s from:(UIView*)vi{
  170. UIImage* image = [ImageResize getImageFromView:vi];
  171. NSData* data = UIImageJPEGRepresentation(image,1);
  172. [data writeToFile:s atomically:YES];
  173. return YES;
  174. }
  175. + (BOOL) saveImageToSize:(NSString*)fromfile toFile:(NSString*)toFile toWidth:(int)toWidth toHeight:(int)toHeight{
  176. CGSize toSize;
  177. toSize.width = toWidth;
  178. toSize.height = toHeight;
  179. NSString* s = fromfile;
  180. UIImage* image = [UIImage imageWithContentsOfFile:s];
  181. image = [ImageResize image:image fitInSize:toSize];
  182. s = toFile;
  183. NSData* data = UIImageJPEGRepresentation(image,0.6);
  184. [data writeToFile:s atomically:YES];
  185. return YES;
  186. }
  187. @end