Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
  2.  
  3. // Log out the image size
  4. NSLog(@"%lu KB",(imageData.length/1024));
  5.  
  6. // Check if the image size is too large
  7. if ((imageData.length/1024) >= 1024) {
  8.  
  9. while ((imageData.length/1024) >= 1024) {
  10. NSLog(@"While start - The imagedata size is currently: %f KB",roundf((imageData.length/1024)));
  11.  
  12. // While the imageData is too large scale down the image
  13.  
  14. // Get the current image size
  15. CGSize currentSize = CGSizeMake(image.size.width, image.size.height);
  16.  
  17. // Resize the image
  18. image = [image resizedImage:CGSizeMake(roundf(((currentSize.width/100)*80)), roundf(((currentSize.height/100)*80))) interpolationQuality:kMESImageQuality];
  19.  
  20. // Pass the NSData out again
  21. imageData = UIImageJPEGRepresentation(image, kMESImageQuality);
  22.  
  23. }
  24. }
  25.  
  26. NSData *imageData = UIImageJPEGRepresentation(image, kMESImageQuality);
  27. double factor = 1.0;
  28. double adjustment = 1.0 / sqrt(2.0); // or use 0.8 or whatever you want
  29. CGSize size = image.size;
  30. CGSize currentSize = size;
  31. UIImage *currentImage = image;
  32.  
  33. while (imageData.length >= (1024 * 1024))
  34. {
  35. factor *= adjustment;
  36. currentSize = CGSizeMake(roundf(size.width * factor), roundf(size.height * factor));
  37. currentImage = [image resizedImage:currentSize interpolationQuality:kMESImageQuality];
  38. imageData = UIImageJPEGRepresentation(currentImage, kMESImageQuality);
  39. }
  40.  
  41. // Check if the image size is too large
  42. if ((imageData.length/1024) >= 1024) {
  43.  
  44. while ((imageData.length/1024) >= 1024) {
  45. NSLog(@"While start - The imagedata size is currently: %f KB",roundf((imageData.length/1024)));
  46.  
  47. // While the imageData is too large scale down the image
  48.  
  49. // Get the current image size
  50. CGSize currentSize = CGSizeMake(image.size.width, image.size.height);
  51.  
  52. // Resize the image
  53. image = [image resizedImage:CGSizeMake(roundf(((currentSize.width/100)*80)), roundf(((currentSize.height/100)*80))) interpolationQuality:kMESImageQuality];
  54.  
  55. // Pass the NSData out again
  56. imageData = UIImageJPEGRepresentation(image, kMESImageQuality);
  57.  
  58. }
  59. }
  60.  
  61. // Returns a rescaled copy of the image, taking into account its orientation
  62. // The image will be scaled disproportionately if necessary to fit the bounds specified by the parameter
  63. - (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
  64. BOOL drawTransposed;
  65.  
  66. switch (self.imageOrientation) {
  67. case UIImageOrientationLeft:
  68. case UIImageOrientationLeftMirrored:
  69. case UIImageOrientationRight:
  70. case UIImageOrientationRightMirrored:
  71. drawTransposed = YES;
  72. break;
  73.  
  74. default:
  75. drawTransposed = NO;
  76. }
  77.  
  78. return [self resizedImage:newSize
  79. transform:[self transformForOrientation:newSize]
  80. drawTransposed:drawTransposed
  81. interpolationQuality:quality];
  82. }
  83.  
  84. // Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
  85. // The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
  86. // If the new size is not integral, it will be rounded up
  87. - (UIImage *)resizedImage:(CGSize)newSize
  88. transform:(CGAffineTransform)transform
  89. drawTransposed:(BOOL)transpose
  90. interpolationQuality:(CGInterpolationQuality)quality {
  91. CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
  92. CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
  93. CGImageRef imageRef = self.CGImage;
  94.  
  95. // Build a context that's the same dimensions as the new size
  96. CGContextRef bitmap = CGBitmapContextCreate(NULL,
  97. newRect.size.width,
  98. newRect.size.height,
  99. CGImageGetBitsPerComponent(imageRef),
  100. 0,
  101. CGImageGetColorSpace(imageRef),
  102. CGImageGetBitmapInfo(imageRef));
  103.  
  104. // Rotate and/or flip the image if required by its orientation
  105. CGContextConcatCTM(bitmap, transform);
  106.  
  107. // Set the quality level to use when rescaling
  108. CGContextSetInterpolationQuality(bitmap, quality);
  109.  
  110. // Draw into the context; this scales the image
  111. CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);
  112.  
  113. // Get the resized image from the context and a UIImage
  114. CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
  115. UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
  116.  
  117. // Clean up
  118. CGContextRelease(bitmap);
  119. CGImageRelease(newImageRef);
  120.  
  121. return newImage;
  122. }
  123.  
  124. // Returns an affine transform that takes into account the image orientation when drawing a scaled image
  125. - (CGAffineTransform)transformForOrientation:(CGSize)newSize {
  126. CGAffineTransform transform = CGAffineTransformIdentity;
  127.  
  128. switch (self.imageOrientation) {
  129. case UIImageOrientationDown: // EXIF = 3
  130. case UIImageOrientationDownMirrored: // EXIF = 4
  131. transform = CGAffineTransformTranslate(transform, newSize.width, newSize.height);
  132. transform = CGAffineTransformRotate(transform, M_PI);
  133. break;
  134.  
  135. case UIImageOrientationLeft: // EXIF = 6
  136. case UIImageOrientationLeftMirrored: // EXIF = 5
  137. transform = CGAffineTransformTranslate(transform, newSize.width, 0);
  138. transform = CGAffineTransformRotate(transform, M_PI_2);
  139. break;
  140.  
  141. case UIImageOrientationRight: // EXIF = 8
  142. case UIImageOrientationRightMirrored: // EXIF = 7
  143. transform = CGAffineTransformTranslate(transform, 0, newSize.height);
  144. transform = CGAffineTransformRotate(transform, -M_PI_2);
  145. break;
  146. default:
  147. break;
  148. }
  149.  
  150. switch (self.imageOrientation) {
  151. case UIImageOrientationUpMirrored: // EXIF = 2
  152. case UIImageOrientationDownMirrored: // EXIF = 4
  153. transform = CGAffineTransformTranslate(transform, newSize.width, 0);
  154. transform = CGAffineTransformScale(transform, -1, 1);
  155. break;
  156.  
  157. case UIImageOrientationLeftMirrored: // EXIF = 5
  158. case UIImageOrientationRightMirrored: // EXIF = 7
  159. transform = CGAffineTransformTranslate(transform, newSize.height, 0);
  160. transform = CGAffineTransformScale(transform, -1, 1);
  161. break;
  162. default:
  163. break;
  164. }
  165.  
  166. return transform;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement