UIImage-Extensions.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // UIImage-Extensions.m
  3. // CutPicetureDemo
  4. //
  5. // Created by yang kong on 12-6-27.
  6. // Copyright 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "UIImage-Extensions.h"
  9. CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
  10. CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
  11. @implementation UIImage (CS_Extensions)
  12. -(UIImage *)imageAtRect:(CGRect)rect
  13. {
  14. CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
  15. UIImage* subImage = [UIImage imageWithCGImage: imageRef];
  16. CGImageRelease(imageRef);
  17. return subImage;
  18. }
  19. - (UIImage *)imageByScalingProportionallyToMinimumSize:(CGSize)targetSize {
  20. UIImage *sourceImage = self;
  21. UIImage *newImage = nil;
  22. CGSize imageSize = sourceImage.size;
  23. CGFloat width = imageSize.width;
  24. CGFloat height = imageSize.height;
  25. CGFloat targetWidth = targetSize.width;
  26. CGFloat targetHeight = targetSize.height;
  27. CGFloat scaleFactor = 0.0;
  28. CGFloat scaledWidth = targetWidth;
  29. CGFloat scaledHeight = targetHeight;
  30. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  31. if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
  32. CGFloat widthFactor = targetWidth / width;
  33. CGFloat heightFactor = targetHeight / height;
  34. if (widthFactor > heightFactor)
  35. scaleFactor = widthFactor;
  36. else
  37. scaleFactor = heightFactor;
  38. scaledWidth = width * scaleFactor;
  39. scaledHeight = height * scaleFactor;
  40. // center the image
  41. if (widthFactor > heightFactor) {
  42. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  43. } else if (widthFactor < heightFactor) {
  44. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  45. }
  46. }
  47. // this is actually the interesting part:
  48. UIGraphicsBeginImageContext(targetSize);
  49. CGRect thumbnailRect = CGRectZero;
  50. thumbnailRect.origin = thumbnailPoint;
  51. thumbnailRect.size.width = scaledWidth;
  52. thumbnailRect.size.height = scaledHeight;
  53. [sourceImage drawInRect:thumbnailRect];
  54. newImage = UIGraphicsGetImageFromCurrentImageContext();
  55. UIGraphicsEndImageContext();
  56. if(newImage == nil) NSLog(@"could not scale image");
  57. return newImage ;
  58. }
  59. - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
  60. UIImage *sourceImage = self;
  61. UIImage *newImage = nil;
  62. CGSize imageSize = sourceImage.size;
  63. CGFloat width = imageSize.width;
  64. CGFloat height = imageSize.height;
  65. CGFloat targetWidth = targetSize.width;
  66. CGFloat targetHeight = targetSize.height;
  67. CGFloat scaleFactor = 0.0;
  68. CGFloat scaledWidth = targetWidth;
  69. CGFloat scaledHeight = targetHeight;
  70. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  71. if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
  72. CGFloat widthFactor = targetWidth / width;
  73. CGFloat heightFactor = targetHeight / height;
  74. if (widthFactor < heightFactor)
  75. scaleFactor = widthFactor;
  76. else
  77. scaleFactor = heightFactor;
  78. scaledWidth = width * scaleFactor;
  79. scaledHeight = height * scaleFactor;
  80. // center the image
  81. if (widthFactor < heightFactor) {
  82. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  83. } else if (widthFactor > heightFactor) {
  84. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  85. }
  86. }
  87. // this is actually the interesting part:
  88. UIGraphicsBeginImageContext(targetSize);
  89. CGRect thumbnailRect = CGRectZero;
  90. thumbnailRect.origin = thumbnailPoint;
  91. thumbnailRect.size.width = scaledWidth;
  92. thumbnailRect.size.height = scaledHeight;
  93. [sourceImage drawInRect:thumbnailRect];
  94. newImage = UIGraphicsGetImageFromCurrentImageContext();
  95. UIGraphicsEndImageContext();
  96. if(newImage == nil) NSLog(@"could not scale image");
  97. return newImage ;
  98. }
  99. - (UIImage *)imageByScalingToSize:(CGSize)targetSize {
  100. UIImage *sourceImage = self;
  101. UIImage *newImage = nil;
  102. // CGSize imageSize = sourceImage.size;
  103. // CGFloat width = imageSize.width;
  104. // CGFloat height = imageSize.height;
  105. CGFloat targetWidth = targetSize.width;
  106. CGFloat targetHeight = targetSize.height;
  107. // CGFloat scaleFactor = 0.0;
  108. CGFloat scaledWidth = targetWidth;
  109. CGFloat scaledHeight = targetHeight;
  110. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  111. // this is actually the interesting part:
  112. UIGraphicsBeginImageContext(targetSize);
  113. CGRect thumbnailRect = CGRectZero;
  114. thumbnailRect.origin = thumbnailPoint;
  115. thumbnailRect.size.width = scaledWidth;
  116. thumbnailRect.size.height = scaledHeight;
  117. [sourceImage drawInRect:thumbnailRect];
  118. newImage = UIGraphicsGetImageFromCurrentImageContext();
  119. UIGraphicsEndImageContext();
  120. if(newImage == nil) NSLog(@"could not scale image");
  121. return newImage ;
  122. }
  123. - (UIImage *)imageByScalingToScale:(CGFloat)toScale{
  124. UIImage *sourceImage = self;
  125. CGSize targetSize = CGSizeMake(self.size.width*toScale, self.size.height*toScale);
  126. UIGraphicsBeginImageContext(targetSize);
  127. [sourceImage drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
  128. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  129. UIGraphicsEndImageContext();
  130. if (scaledImage == nil) {
  131. NSLog(@"could not scale image");
  132. }
  133. return scaledImage;
  134. }
  135. - (UIImage *)cutImageBy:(CGSize)oSize{
  136. CGSize newSize;
  137. CGImageRef imageRef = nil;
  138. if ((self.size.width / self.size.height) < (oSize.width / oSize.height)) {
  139. newSize.width = self.size.width;
  140. newSize.height = self.size.width * oSize.height / oSize.width;
  141. imageRef = CGImageCreateWithImageInRect([self CGImage], CGRectMake(0, fabs(self.size.height - newSize.height) / 2, newSize.width, newSize.height));
  142. } else {
  143. newSize.height = self.size.height;
  144. newSize.width = self.size.height * oSize.width / oSize.height;
  145. imageRef = CGImageCreateWithImageInRect([self CGImage], CGRectMake(fabs(self.size.width - newSize.width) / 2, 0, newSize.width, newSize.height));
  146. }
  147. return [UIImage imageWithCGImage:imageRef];
  148. }
  149. - (UIImage *)imageRotatedByRadians:(CGFloat)radians
  150. {
  151. return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
  152. }
  153. - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
  154. {
  155. // calculate the size of the rotated view's containing box for our drawing space
  156. UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
  157. CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
  158. rotatedViewBox.transform = t;
  159. CGSize rotatedSize = rotatedViewBox.frame.size;
  160. // [rotatedViewBox release];
  161. // Create the bitmap context
  162. UIGraphicsBeginImageContext(rotatedSize);
  163. CGContextRef bitmap = UIGraphicsGetCurrentContext();
  164. // Move the origin to the middle of the image so we will rotate and scale around the center.
  165. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
  166. // // Rotate the image context
  167. CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
  168. // Now, draw the rotated/scaled image into the context
  169. CGContextScaleCTM(bitmap, 1.0, -1.0);
  170. CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
  171. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  172. UIGraphicsEndImageContext();
  173. return newImage;
  174. }
  175. @end;