Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  2. {
  3. //Copy the image to the userImage variable
  4. [picker dismissModalViewControllerAnimated:YES];
  5. userImage = nil;
  6.  
  7. //Rotate & resize image
  8. userImage = [self resizeAndRotatePhoto:(UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]];
  9. NSLog(@"Userimage size, width: %f , height: %f", userImage.size.width , userImage.size.height);
  10.  
  11. //Update the image form field with the image and set the image in the controller
  12. NSLog(@"Button size is height: %f , width: %f" , userImageAvatarButton.frame.size.height , userImageAvatarButton.frame.size.width);
  13. [userImageAvatarButton.layer setMasksToBounds:YES];
  14. [userImageAvatarButton.layer setCornerRadius:3.0];
  15. [userImageAvatarButton setImage:userImage forState:UIControlStateNormal];
  16. }
  17.  
  18. 2012-05-07 17:38:07.995 NewApp[10666:707] Userimage size, width: 1936.000000 , height: 1936.000000
  19. 2012-05-07 17:38:08.000 NewApp[10666:707] Button size is height: 60.000000 , width: 60.000000
  20.  
  21. - (UIImage *)resizeAndRotatePhoto:(UIImage *)source
  22. {
  23. if( source.imageOrientation == UIImageOrientationRight )
  24. {
  25. source = [self rotateImage:source byDegrees:90];
  26. }
  27. if( userImage.imageOrientation == UIImageOrientationLeft)
  28. {
  29. source = [self rotateImage:source byDegrees:-90];
  30. }
  31.  
  32. CGFloat x,y;
  33. CGFloat size;
  34. if( source.size.width > source.size.height ){
  35. size = source.size.height;
  36. x = (source.size.width - source.size.height)/2;
  37. y = 0;
  38. }
  39. else {
  40. size = source.size.width;
  41. x = 0;
  42. y = (source.size.height - source.size.width)/2;
  43. }
  44.  
  45.  
  46. CGImageRef imageRef = CGImageCreateWithImageInRect([source CGImage], CGRectMake(x,y,size,size) );
  47. return [UIImage imageWithCGImage:imageRef];
  48. }
  49.  
  50. // UIImage+SimpleResize.m
  51. //
  52. // Created by Robert Ryan on 5/19/11.
  53.  
  54. #import "UIImage+SimpleResize.h"
  55.  
  56. @implementation UIImage (SimpleResize)
  57.  
  58. - (UIImage*)imageByScalingToSize:(CGSize)newSize contentMode:(UIViewContentMode)contentMode
  59. {
  60. if (contentMode == UIViewContentModeScaleToFill)
  61. {
  62. return [self imageByScalingToFillSize:newSize];
  63. }
  64. else if ((contentMode == UIViewContentModeScaleAspectFill) ||
  65. (contentMode == UIViewContentModeScaleAspectFit))
  66. {
  67. CGFloat horizontalRatio = self.size.width / newSize.width;
  68. CGFloat verticalRatio = self.size.height / newSize.height;
  69. CGFloat ratio;
  70.  
  71. if (contentMode == UIViewContentModeScaleAspectFill)
  72. ratio = MIN(horizontalRatio, verticalRatio);
  73. else
  74. ratio = MAX(horizontalRatio, verticalRatio);
  75.  
  76. CGSize sizeForAspectScale = CGSizeMake(self.size.width / ratio, self.size.height / ratio);
  77.  
  78. UIImage *image = [self imageByScalingToFillSize:sizeForAspectScale];
  79.  
  80. // if we're doing aspect fill, then the image still needs to be cropped
  81.  
  82. if (contentMode == UIViewContentModeScaleAspectFill)
  83. {
  84. CGRect subRect = CGRectMake(floor((sizeForAspectScale.width - newSize.width) / 2.0),
  85. floor((sizeForAspectScale.height - newSize.height) / 2.0),
  86. newSize.width,
  87. newSize.height);
  88. image = [image imageByCroppingToBounds:subRect];
  89. }
  90.  
  91. return image;
  92. }
  93.  
  94. return nil;
  95. }
  96.  
  97. - (UIImage *)imageByCroppingToBounds:(CGRect)bounds
  98. {
  99. CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
  100. UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
  101. CGImageRelease(imageRef);
  102. return croppedImage;
  103. }
  104.  
  105. - (UIImage*)imageByScalingToFillSize:(CGSize)newSize
  106. {
  107. UIGraphicsBeginImageContext(newSize);
  108. [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
  109. UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
  110. UIGraphicsEndImageContext();
  111.  
  112. return image;
  113. }
  114.  
  115. - (UIImage*)imageByScalingAspectFillSize:(CGSize)newSize
  116. {
  117. return [self imageByScalingToSize:newSize contentMode:UIViewContentModeScaleAspectFill];
  118. }
  119.  
  120. - (UIImage*)imageByScalingAspectFitSize:(CGSize)newSize
  121. {
  122. return [self imageByScalingToSize:newSize contentMode:UIViewContentModeScaleAspectFit];
  123. }
  124.  
  125. @end
  126.  
  127. // UIImage+SimpleResize.h
  128. //
  129. // Created by Robert Ryan on 5/19/11.
  130.  
  131. #import <Foundation/Foundation.h>
  132.  
  133. /** Image resizing category.
  134. *
  135. * Modified by Robert Ryan on 5/19/11.
  136. *
  137. * Inspired by http://ofcodeandmen.poltras.com/2008/10/30/undocumented-uiimage-resizing/
  138. * but adjusted to support AspectFill and AspectFit modes.
  139. */
  140.  
  141. @interface UIImage (SimpleResize)
  142.  
  143. /** Resize the image to be the required size, stretching it as needed.
  144. *
  145. * @param newSize The new size of the image.
  146. * @param contentMode The `UIViewContentMode` to be applied when resizing image.
  147. * Either `UIViewContentModeScaleToFill`, `UIViewContentModeScaleAspectFill`, or
  148. * `UIViewContentModeScaleAspectFit`.
  149. *
  150. * @return Return `UIImage` of resized image.
  151. */
  152.  
  153. - (UIImage*)imageByScalingToSize:(CGSize)newSize contentMode:(UIViewContentMode)contentMode;
  154.  
  155. /** Crop the image to be the required size.
  156. *
  157. * @param bounds The bounds to which the new image should be cropped.
  158. *
  159. * @return Cropped `UIImage`.
  160. */
  161.  
  162. - (UIImage *)imageByCroppingToBounds:(CGRect)bounds;
  163.  
  164. /** Resize the image to be the required size, stretching it as needed.
  165. *
  166. * @param newSize The new size of the image.
  167. *
  168. * @return Resized `UIImage` of resized image.
  169. */
  170.  
  171. - (UIImage*)imageByScalingToFillSize:(CGSize)newSize;
  172.  
  173. /** Resize the image to fill the rectange of the specified size, preserving the aspect ratio, trimming if needed.
  174. *
  175. * @param newSize The new size of the image.
  176. *
  177. * @return Return `UIImage` of resized image.
  178. */
  179.  
  180. - (UIImage*)imageByScalingAspectFillSize:(CGSize)newSize;
  181.  
  182. /** Resize the image to fit within the required size, preserving the aspect ratio, with no trimming taking place.
  183. *
  184. * @param newSize The new size of the image.
  185. *
  186. * @return Return `UIImage` of resized image.
  187. */
  188.  
  189. - (UIImage*)imageByScalingAspectFitSize:(CGSize)newSize;
  190.  
  191. @end
  192.  
  193. - (void)resizeImageAtPath:(NSString *)imagePath {
  194. // Create the image source (from path)
  195. CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:imagePath], NULL);
  196.  
  197. // To create image source from UIImage, use this
  198. // NSData* pngData = UIImagePNGRepresentation(image);
  199. // CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)pngData, NULL);
  200.  
  201. // Create thumbnail options
  202. CFDictionaryRef options = (__bridge CFDictionaryRef) @{
  203. (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
  204. (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
  205. (id) kCGImageSourceThumbnailMaxPixelSize : @(640)
  206. };
  207. // Generate the thumbnail
  208. CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
  209. CFRelease(src);
  210. // Write the thumbnail at path
  211. CGImageWriteToFile(thumbnail, imagePath);
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement