UIImage+Blur.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* This file is adapted from DKLiveBlur - https://github.com/kronik/DKLiveBlur
  2. *
  3. * Copyright (C) 2013 by Dmitry Klimkin
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE. */
  22. #import <Accelerate/Accelerate.h>
  23. @implementation UIImage (Blur)
  24. + (UIImage *)applyBlurOnImage:(UIImage *)imageToBlur withRadius:(CGFloat)blurRadius {
  25. if (!imageToBlur)
  26. return nil;
  27. if ((blurRadius < 0.0f) || (blurRadius > 1.0f)) {
  28. blurRadius = 0.5f;
  29. }
  30. int boxSize = (int)(blurRadius * 100);
  31. boxSize -= (boxSize % 2) + 1;
  32. CGImageRef rawImage = imageToBlur.CGImage;
  33. vImage_Buffer inBuffer, outBuffer;
  34. vImage_Error error;
  35. void *pixelBuffer;
  36. CGDataProviderRef inProvider = CGImageGetDataProvider(rawImage);
  37. CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
  38. inBuffer.width = CGImageGetWidth(rawImage);
  39. inBuffer.height = CGImageGetHeight(rawImage);
  40. inBuffer.rowBytes = CGImageGetBytesPerRow(rawImage);
  41. inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
  42. pixelBuffer = malloc(CGImageGetBytesPerRow(rawImage) * CGImageGetHeight(rawImage));
  43. outBuffer.data = pixelBuffer;
  44. outBuffer.width = CGImageGetWidth(rawImage);
  45. outBuffer.height = CGImageGetHeight(rawImage);
  46. outBuffer.rowBytes = CGImageGetBytesPerRow(rawImage);
  47. error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL,
  48. 0, 0, boxSize, boxSize, NULL,
  49. kvImageEdgeExtend);
  50. if (error)
  51. APLog(@"blur error: convolution failed");
  52. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  53. CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
  54. outBuffer.width,
  55. outBuffer.height,
  56. 8,
  57. outBuffer.rowBytes,
  58. colorSpace,
  59. CGImageGetBitmapInfo(imageToBlur.CGImage));
  60. CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  61. UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
  62. //clean up
  63. CGContextRelease(ctx);
  64. CGColorSpaceRelease(colorSpace);
  65. free(pixelBuffer);
  66. CFRelease(inBitmapData);
  67. CGImageRelease(imageRef);
  68. return returnImage;
  69. }
  70. @end