Guest User

Untitled

a guest
Jul 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. - (void)saveImageToDisk:(UIImage *)newImage
  2. {
  3. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  4. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  5.  
  6. UIImageOrientation orientation = [newImage imageOrientation];
  7.  
  8. // Scale image first
  9. CGSize imageSize = [newImage size];
  10. BOOL imageIsRotated = NO;
  11. if(orientation == UIImageOrientationLeft
  12. || orientation == UIImageOrientationRight
  13. || orientation == UIImageOrientationLeftMirrored
  14. || orientation == UIImageOrientationRightMirrored)
  15. imageIsRotated = YES;
  16.  
  17. float w1 = imageSize.width;
  18. float h1 = imageSize.height;
  19. if(imageIsRotated)
  20. {
  21. while(h1 > 640 || w1 > 480)
  22. {
  23. h1--;
  24. w1--;
  25. }
  26. }
  27. else
  28. {
  29. while(w1 > 640 || h1 > 480)
  30. {
  31. h1--;
  32. w1--;
  33. }
  34. }
  35. float s1 = (float)h1 / imageSize.height;
  36. float s2 = (float)w1 / imageSize.width;
  37. float scale = (s1 > s2 ? s1 : s2);
  38.  
  39. // Draw scaled version..
  40. int w = imageSize.width * scale;
  41. int h = imageSize.height * scale;
  42. if(imageIsRotated)
  43. {
  44. w = imageSize.height * scale;
  45. h = imageSize.width * scale;
  46. }
  47. int larger = w;
  48. if(h > larger)
  49. larger = h;
  50. CGContextRef ctx = CGBitmapContextCreate(NULL, larger, larger, 8, larger * 4, colorSpace, kCGImageAlphaPremultipliedLast);
  51. configureCTM(ctx, orientation, CGSizeMake(larger, larger));
  52. CGContextDrawImage(ctx, CGRectMake(0, 0, w, h), [newImage CGImage]);
  53. CGImageRef scaledImage = CGBitmapContextCreateImage(ctx);
  54. CGContextRelease(ctx);
  55.  
  56. CGRect imageRect = CGRectMake(0, 0, w, h);
  57. if(imageIsRotated)
  58. imageRect = CGRectMake(0, 0, h, w);
  59. CGImageRef transformedImage = CGImageCreateWithImageInRect(scaledImage, imageRect);
  60.  
  61. ctx = CGBitmapContextCreate(NULL, PCMaxImageSize.width, PCMaxImageSize.height, 8, PCMaxImageSize.width * 4, colorSpace, kCGImageAlphaPremultipliedLast);
  62. CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
  63. CGContextFillRect(ctx, CGRectMake(0, 0, PCMaxImageSize.width, PCMaxImageSize.height));
  64. float xt = (PCMaxImageSize.width - imageRect.size.width) / 2.0;
  65. float yt = (PCMaxImageSize.height - imageRect.size.height) / 2.0;
  66. CGContextDrawImage(ctx, CGRectMake(xt, yt, imageRect.size.width, imageRect.size.height), transformedImage);
  67. CGImageRef finalImage = CGBitmapContextCreateImage(ctx);
  68.  
  69. fullImage = [[UIImage imageWithCGImage:finalImage] retain];
  70.  
  71. CGContextRelease(ctx);
  72. CGImageRelease(scaledImage);
  73. CGImageRelease(transformedImage);
  74. CGImageRelease(finalImage);
  75. CGColorSpaceRelease(colorSpace);
  76. [pool drain];
  77. }
Add Comment
Please, Sign In to add comment