Guest User

Untitled

a guest
Dec 9th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. //
  2. // UIImage-Extensions.m
  3. //
  4. // Created by Hardy Macia on 7/1/09.
  5. // Copyright 2009 Catamount Software. All rights reserved.
  6. //
  7.  
  8. #import "UIImage-Extensions.h"
  9.  
  10. CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
  11. CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
  12.  
  13. @implementation UIImage (CS_Extensions)
  14.  
  15. -(UIImage *)imageAtRect:(CGRect)rect
  16. {
  17.  
  18. CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
  19. UIImage* subImage = [UIImage imageWithCGImage: imageRef];
  20. CGImageRelease(imageRef);
  21.  
  22. return subImage;
  23.  
  24. }
  25.  
  26. - (UIImage *)imageByScalingProportionallyToMinimumSize:(CGSize)targetSize {
  27.  
  28. UIImage *sourceImage = self;
  29. UIImage *newImage = nil;
  30.  
  31. CGSize imageSize = sourceImage.size;
  32. CGFloat width = imageSize.width;
  33. CGFloat height = imageSize.height;
  34.  
  35. CGFloat targetWidth = targetSize.width;
  36. CGFloat targetHeight = targetSize.height;
  37.  
  38. CGFloat scaleFactor = 0.0;
  39. CGFloat scaledWidth = targetWidth;
  40. CGFloat scaledHeight = targetHeight;
  41.  
  42. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  43.  
  44. if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
  45.  
  46. CGFloat widthFactor = targetWidth / width;
  47. CGFloat heightFactor = targetHeight / height;
  48.  
  49. if (widthFactor > heightFactor)
  50. scaleFactor = widthFactor;
  51. else
  52. scaleFactor = heightFactor;
  53.  
  54. scaledWidth = width * scaleFactor;
  55. scaledHeight = height * scaleFactor;
  56.  
  57. // center the image
  58.  
  59. if (widthFactor > heightFactor) {
  60. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  61. } else if (widthFactor < heightFactor) {
  62. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  63. }
  64. }
  65.  
  66.  
  67. // this is actually the interesting part:
  68.  
  69. UIGraphicsBeginImageContext(targetSize);
  70.  
  71. CGRect thumbnailRect = CGRectZero;
  72. thumbnailRect.origin = thumbnailPoint;
  73. thumbnailRect.size.width = scaledWidth;
  74. thumbnailRect.size.height = scaledHeight;
  75.  
  76. [sourceImage drawInRect:thumbnailRect];
  77.  
  78. newImage = UIGraphicsGetImageFromCurrentImageContext();
  79. UIGraphicsEndImageContext();
  80.  
  81. if(newImage == nil) NSLog(@"could not scale image");
  82.  
  83.  
  84. return newImage ;
  85. }
  86.  
  87.  
  88. - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
  89.  
  90. UIImage *sourceImage = self;
  91. UIImage *newImage = nil;
  92.  
  93. CGSize imageSize = sourceImage.size;
  94. CGFloat width = imageSize.width;
  95. CGFloat height = imageSize.height;
  96.  
  97. CGFloat targetWidth = targetSize.width;
  98. CGFloat targetHeight = targetSize.height;
  99.  
  100. CGFloat scaleFactor = 0.0;
  101. CGFloat scaledWidth = targetWidth;
  102. CGFloat scaledHeight = targetHeight;
  103.  
  104. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  105.  
  106. if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
  107.  
  108. CGFloat widthFactor = targetWidth / width;
  109. CGFloat heightFactor = targetHeight / height;
  110.  
  111. if (widthFactor < heightFactor)
  112. scaleFactor = widthFactor;
  113. else
  114. scaleFactor = heightFactor;
  115.  
  116. scaledWidth = width * scaleFactor;
  117. scaledHeight = height * scaleFactor;
  118.  
  119. // center the image
  120.  
  121. if (widthFactor < heightFactor) {
  122. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  123. } else if (widthFactor > heightFactor) {
  124. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  125. }
  126. }
  127.  
  128.  
  129. // this is actually the interesting part:
  130.  
  131. UIGraphicsBeginImageContext(targetSize);
  132.  
  133. CGRect thumbnailRect = CGRectZero;
  134. thumbnailRect.origin = thumbnailPoint;
  135. thumbnailRect.size.width = scaledWidth;
  136. thumbnailRect.size.height = scaledHeight;
  137.  
  138. [sourceImage drawInRect:thumbnailRect];
  139.  
  140. newImage = UIGraphicsGetImageFromCurrentImageContext();
  141. UIGraphicsEndImageContext();
  142.  
  143. if(newImage == nil) NSLog(@"could not scale image");
  144.  
  145.  
  146. return newImage ;
  147. }
  148.  
  149.  
  150. - (UIImage *)imageByScalingToSize:(CGSize)targetSize {
  151.  
  152. UIImage *sourceImage = self;
  153. UIImage *newImage = nil;
  154.  
  155. // CGSize imageSize = sourceImage.size;
  156. // CGFloat width = imageSize.width;
  157. // CGFloat height = imageSize.height;
  158.  
  159. CGFloat targetWidth = targetSize.width;
  160. CGFloat targetHeight = targetSize.height;
  161.  
  162. // CGFloat scaleFactor = 0.0;
  163. CGFloat scaledWidth = targetWidth;
  164. CGFloat scaledHeight = targetHeight;
  165.  
  166. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  167.  
  168. // this is actually the interesting part:
  169.  
  170. UIGraphicsBeginImageContext(targetSize);
  171.  
  172. CGRect thumbnailRect = CGRectZero;
  173. thumbnailRect.origin = thumbnailPoint;
  174. thumbnailRect.size.width = scaledWidth;
  175. thumbnailRect.size.height = scaledHeight;
  176.  
  177. [sourceImage drawInRect:thumbnailRect];
  178.  
  179. newImage = UIGraphicsGetImageFromCurrentImageContext();
  180. UIGraphicsEndImageContext();
  181.  
  182. if(newImage == nil) NSLog(@"could not scale image");
  183.  
  184.  
  185. return newImage ;
  186. }
  187.  
  188.  
  189. - (UIImage *)imageRotatedByRadians:(CGFloat)radians
  190. {
  191. return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
  192. }
  193.  
  194. - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
  195. {
  196. // calculate the size of the rotated view's containing box for our drawing space
  197. UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
  198. CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
  199. rotatedViewBox.transform = t;
  200. CGSize rotatedSize = rotatedViewBox.frame.size;
  201. [rotatedViewBox release];
  202.  
  203. // Create the bitmap context
  204. UIGraphicsBeginImageContext(rotatedSize);
  205. CGContextRef bitmap = UIGraphicsGetCurrentContext();
  206.  
  207. // Move the origin to the middle of the image so we will rotate and scale around the center.
  208. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
  209.  
  210. // // Rotate the image context
  211. CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
  212.  
  213. // Now, draw the rotated/scaled image into the context
  214. CGContextScaleCTM(bitmap, 1.0, -1.0);
  215. CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
  216.  
  217. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  218. UIGraphicsEndImageContext();
  219. return newImage;
  220.  
  221. }
  222.  
  223. @end;
Add Comment
Please, Sign In to add comment