So I have an app where you move images around in certain positions and layer them. Its coming along well but an issue I keep running into is a lot of my images have white spaces around them (they use to be jpgs), the whitespace is always hexcode #FFFFFF pure whitespace, so I was wondering is there a way in objective-c to mask all of a hexcode in an image? I would manually edit the images but there are thousands of them from a third party. Any ideas?
-
So you could convert to an RGBA colorspace, and then change all #FFFFFF pixels to have an alpha of 0 – j_mcnally Feb 07 '15 at 00:17
-
See: http://stackoverflow.com/questions/12396236/ios-change-the-colors-of-a-uiimage – j_mcnally Feb 07 '15 at 00:18
-
http://stackoverflow.com/questions/633722/how-to-make-one-color-transparent-on-a-uiimage – Rory McKinnel Feb 07 '15 at 00:18
2 Answers
I found this awesome method here that you can place in your current .h file:
+(UIImage *)changeWhiteColorTransparent: (UIImage *)image
{
CGImageRef rawImageRef=image.CGImage;
const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
UIGraphicsBeginImageContext(image.size);
CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
{
//if in iphone
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
}
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(maskedImageRef);
UIGraphicsEndImageContext();
return result;
}
So simply pass your image to this method like so:
UIImage *newImage = [self changeWhiteColorTransparent: yourOldImage];

- 1
- 1

- 2,565
- 17
- 38
-
Amazing! I ended up having to make the function private and change a few things but this work so well! I wish I could give you more then just an upvote and best answer ! – CMOS Feb 07 '15 at 00:35
-
1To support retina or image scale, replace `UIGraphicsBeginImageContext(image.size);` with `UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);` – Genki Sep 07 '16 at 05:35
-
You can use ImageMagick's API to remove same-color space from image edges.
On the ImageMagick command line, it is as easy as:
convert input.jpg -trim +repage output.png
This -trim
operation will automatically remove pixel rows and columns until it meets a pixel that is no longer the same color as the outmost rows/columns.
If your outlaying pixels do not use the exactly same colors, you can add a 'fuzz factor' with -fuzz X%
. This will remove all pixels which are within a color distance of X%
:
convert input.tiff -trim -fuzz 5% +repage output.gif
convert input.png -trim -fuzz 9% +repage output.png
(As you can see, the very same command can autodetect requested input and output file formats and transform between them too, if you need that.)
Usually, what you can achieve via the ImageMagick command line, you can also achieve in your own programs when using the API.
Here is a list with all currently available API interfaces. It contains the links to the specific web resources:
- G2F (Ada),
- MagickCore (C),
- MagickWand (C),
- ChMagick (Ch),
- ImageMagickObject (COM+),
- Magick++ (C++),
- JMagick (Java),
- L-Magick (Lisp),
- Lua (Lua),
- NMagick (Neko/Haxe),
- Magick.NET (.NET),
- PascalMagick (Pascal),
- PerlMagick (Perl),
- MagickWand for PHP (PHP),
- IMagick (PHP),
- PythonMagick (Python),
- RMagick (Ruby),
- TclMagick (Tcl/TK).
Caution: Not all of these APIs are equally well developed, maintained or complete.

- 31,714
- 9
- 78
- 100

- 86,724
- 23
- 248
- 345
-
Hi Kurt Pfeifle ,I am working magic wand like application using objective c.I am failed to implement Or use these api could you please help on this.Can you give an example application.that will be useful to save my time. – Lakshmi Keerthana Siddu Jan 31 '17 at 04:39