Advertisement
Guest User

Untitled

a guest
May 4th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. -(UIImage*)removeColorFromImage:(UIImage*)sourceImage grayLevel:(int)grayLevel
  2. {
  3. int width = sourceImage.size.width * sourceImage.scale;
  4. int height = sourceImage.size.height * sourceImage.scale;
  5. CGFloat scale = sourceImage.scale;
  6.  
  7. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  8. CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);
  9. CGColorSpaceRelease(colorSpace);
  10.  
  11. CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceImage.CGImage);
  12.  
  13. unsigned int *colorData = CGBitmapContextGetData(context);
  14.  
  15. for (int i = 0; i < width * height; i++)
  16. {
  17. unsigned int color = *colorData;
  18.  
  19. short a = color & 0xFF;
  20. short r = (color >> 8) & 0xFF;
  21. short g = (color >> 16) & 0xFF;
  22. short b = (color >> 24) & 0xFF;
  23.  
  24. if ((r == grayLevel) && (g == grayLevel) && (b == grayLevel))
  25. {
  26. a = r = g = b = 0;
  27. *colorData = (unsigned int)(r << 8) + ((unsigned int)(g) << 16) + ((unsigned int)(b) << 24) + ((unsigned int)(a));
  28. }
  29.  
  30. colorData++;
  31. }
  32.  
  33. CGImageRef output = CGBitmapContextCreateImage(context);
  34. UIImage* retImage = [UIImage imageWithCGImage:output scale:scale orientation:UIImageOrientationUp];
  35.  
  36. CGImageRelease(output);
  37. CGContextRelease(context);
  38.  
  39. return retImage;
  40. }
  41.  
  42. var context = CGBitmapContextCreate(nil, width, height, 8, width * 4, colorSpace, CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement