UIImage+Enhancing.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // UIImage+Enhancing.m
  3. // NYXImagesKit
  4. //
  5. // Created by @Nyx0uf on 03/12/11.
  6. // Copyright 2012 Nyx0uf. All rights reserved.
  7. // www.cocoaintheshell.com
  8. //
  9. #import "UIImage+Enhancing.h"
  10. #import <CoreImage/CoreImage.h>
  11. @implementation UIImage (NYX_Enhancing)
  12. -(UIImage*)autoEnhance
  13. {
  14. /// No Core Image, return original image
  15. if (![CIImage class])
  16. return self;
  17. CIImage* ciImage = [[CIImage alloc] initWithCGImage:self.CGImage];
  18. NSArray* adjustments = [ciImage autoAdjustmentFiltersWithOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:kCIImageAutoAdjustRedEye]];
  19. for (CIFilter* filter in adjustments)
  20. {
  21. [filter setValue:ciImage forKey:kCIInputImageKey];
  22. ciImage = filter.outputImage;
  23. }
  24. CIContext* ctx = [CIContext contextWithOptions:nil];
  25. CGImageRef cgImage = [ctx createCGImage:ciImage fromRect:[ciImage extent]];
  26. UIImage* final = [UIImage imageWithCGImage:cgImage];
  27. CGImageRelease(cgImage);
  28. return final;
  29. }
  30. -(UIImage*)redEyeCorrection
  31. {
  32. /// No Core Image, return original image
  33. if (![CIImage class])
  34. return self;
  35. CIImage* ciImage = [[CIImage alloc] initWithCGImage:self.CGImage];
  36. /// Get the filters and apply them to the image
  37. NSArray* filters = [ciImage autoAdjustmentFiltersWithOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:kCIImageAutoAdjustEnhance]];
  38. for (CIFilter* filter in filters)
  39. {
  40. [filter setValue:ciImage forKey:kCIInputImageKey];
  41. ciImage = filter.outputImage;
  42. }
  43. /// Create the corrected image
  44. CIContext* ctx = [CIContext contextWithOptions:nil];
  45. CGImageRef cgImage = [ctx createCGImage:ciImage fromRect:[ciImage extent]];
  46. UIImage* final = [UIImage imageWithCGImage:cgImage];
  47. CGImageRelease(cgImage);
  48. return final;
  49. }
  50. @end