<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to Mask an Image</title>
	<atom:link href="http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 06:31:16 -0600</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: David E. Wheeler</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-66770</link>
		<dc:creator>David E. Wheeler</dc:creator>
		<pubDate>Sun, 05 Feb 2012 08:03:43 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-66770</guid>
		<description>Thanks for the great post. I turned the code into a category on UIImage for easier use:

&lt;pre&gt;

@interface UIImage (Mask)
- (UIImage *)imageWithMask:(UIImage *)maskImage;
@end


@implementation UIImage (Mask)

- (UIImage *)imageWithMask:(UIImage *)maskImage {
	CGImageRef maskRef = maskImage.CGImage;

	CGImageRef mask = CGImageMaskCreate(
        CGImageGetWidth(maskRef),
		CGImageGetHeight(maskRef),
		CGImageGetBitsPerComponent(maskRef),
		CGImageGetBitsPerPixel(maskRef),
		CGImageGetBytesPerRow(maskRef),
		CGImageGetDataProvider(maskRef), NULL, false
    );

    CGImageRef masked = CGImageCreateWithMask(self.CGImage, mask);
    CGImageRelease(mask);
	return [UIImage imageWithCGImage:masked];
}

@end

Enjoy!

David</description>
		<content:encoded><![CDATA[<p>Thanks for the great post. I turned the code into a category on UIImage for easier use:</p>
<pre>

@interface UIImage (Mask)
- (UIImage *)imageWithMask:(UIImage *)maskImage;
@end

@implementation UIImage (Mask)

- (UIImage *)imageWithMask:(UIImage *)maskImage {
	CGImageRef maskRef = maskImage.CGImage;

	CGImageRef mask = CGImageMaskCreate(
        CGImageGetWidth(maskRef),
		CGImageGetHeight(maskRef),
		CGImageGetBitsPerComponent(maskRef),
		CGImageGetBitsPerPixel(maskRef),
		CGImageGetBytesPerRow(maskRef),
		CGImageGetDataProvider(maskRef), NULL, false
    );

    CGImageRef masked = CGImageCreateWithMask(self.CGImage, mask);
    CGImageRelease(mask);
	return [UIImage imageWithCGImage:masked];
}

@end

Enjoy!

David</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marcio Valenzuela</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-66628</link>
		<dc:creator>Marcio Valenzuela</dc:creator>
		<pubDate>Thu, 02 Feb 2012 20:56:17 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-66628</guid>
		<description>I tried this code, but for some reason the masked image shows up black n white.  My image is in color...what could be the problem?

Here is my code:


- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage *imageToMask = [UIImage imageNamed:@&quot;koko.jpg&quot;];
    UIImageView *imageToMaskImgView = [[UIImageView alloc] initWithImage:imageToMask];
    CGRect imgRect = CGRectMake(0, 0, imageToMaskImgView.frame.size.width, imageToMaskImgView.frame.size.height);
    UIView *maskMaster = [[UIView alloc] initWithFrame:imgRect];
    [maskMaster setBackgroundColor:[UIColor whiteColor]];
    [maskMaster addSubview:imageToMaskImgView];
    UIGraphicsBeginImageContext(maskMaster.bounds.size);
    [maskMaster.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSLog(@&quot;%@ is the view image&quot;, viewImage);
    UIImage *bFooImg = [UIImage imageNamed:@&quot;blackapple.png&quot;];
    self.myPic.image = [self maskImage:bFooImg withMask:viewImage];
}

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
	
    CGImageRef maskRef = maskImage.CGImage; 
    
	CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);
    
	CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
    //memman
    CGImageRelease(mask);
    CGImageRelease(maskRef);
	return [UIImage imageWithCGImage:masked];
    
}</description>
		<content:encoded><![CDATA[<p>I tried this code, but for some reason the masked image shows up black n white.  My image is in color&#8230;what could be the problem?</p>
<p>Here is my code:</p>
<p>- (void)viewDidLoad<br />
{<br />
    [super viewDidLoad];<br />
    UIImage *imageToMask = [UIImage imageNamed:@"koko.jpg"];<br />
    UIImageView *imageToMaskImgView = [[UIImageView alloc] initWithImage:imageToMask];<br />
    CGRect imgRect = CGRectMake(0, 0, imageToMaskImgView.frame.size.width, imageToMaskImgView.frame.size.height);<br />
    UIView *maskMaster = [[UIView alloc] initWithFrame:imgRect];<br />
    [maskMaster setBackgroundColor:[UIColor whiteColor]];<br />
    [maskMaster addSubview:imageToMaskImgView];<br />
    UIGraphicsBeginImageContext(maskMaster.bounds.size);<br />
    [maskMaster.layer renderInContext:UIGraphicsGetCurrentContext()];<br />
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();<br />
    UIGraphicsEndImageContext();<br />
    NSLog(@&#8221;%@ is the view image&#8221;, viewImage);<br />
    UIImage *bFooImg = [UIImage imageNamed:@"blackapple.png"];<br />
    self.myPic.image = [self maskImage:bFooImg withMask:viewImage];<br />
}</p>
<p>- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {</p>
<p>    CGImageRef maskRef = maskImage.CGImage; </p>
<p>	CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),<br />
                                        CGImageGetHeight(maskRef),<br />
                                        CGImageGetBitsPerComponent(maskRef),<br />
                                        CGImageGetBitsPerPixel(maskRef),<br />
                                        CGImageGetBytesPerRow(maskRef),<br />
                                        CGImageGetDataProvider(maskRef), NULL, false);</p>
<p>	CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);<br />
    //memman<br />
    CGImageRelease(mask);<br />
    CGImageRelease(maskRef);<br />
	return [UIImage imageWithCGImage:masked];</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harry</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-66081</link>
		<dc:creator>Harry</dc:creator>
		<pubDate>Sun, 22 Jan 2012 15:38:30 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-66081</guid>
		<description>Hi Besterme,
 
Thanks for the detailed code. However i tried the same snippet with in my project. It didn&#039;t work for me Let me explain you the situation in more detailed way. I have one image of a fish which is the main image having a fish image in front but the background is white. I created another image which is a mask which is of the same size of the original image but is of color black. When i execute the following piece of code it gives me a transparent black. 

[self maskImage:[UIImage imageNamed:@&quot;MaskedBlack.png&quot;] withMask:[UIImage imageNamed:@&quot;Angel-fish-300x233.png&quot;]]; 

With the following code i am getting the original image as it is. It does not give me the extracted image

[self maskImage:[UIImage imageNamed:@&quot;Angel-fish-300x233.png&quot;] withMask:[UIImage imageNamed:@&quot;MaskedBlack.png&quot;]]

Pl. let me know if there is any resolution to this.

Note: I want to extract the fish out of the image as it is without any white background.

Thanks
Harry</description>
		<content:encoded><![CDATA[<p>Hi Besterme,</p>
<p>Thanks for the detailed code. However i tried the same snippet with in my project. It didn&#8217;t work for me Let me explain you the situation in more detailed way. I have one image of a fish which is the main image having a fish image in front but the background is white. I created another image which is a mask which is of the same size of the original image but is of color black. When i execute the following piece of code it gives me a transparent black. </p>
<p>[self maskImage:[UIImage imageNamed:@"MaskedBlack.png"] withMask:[UIImage imageNamed:@"Angel-fish-300x233.png"]]; </p>
<p>With the following code i am getting the original image as it is. It does not give me the extracted image</p>
<p>[self maskImage:[UIImage imageNamed:@"Angel-fish-300x233.png"] withMask:[UIImage imageNamed:@"MaskedBlack.png"]]</p>
<p>Pl. let me know if there is any resolution to this.</p>
<p>Note: I want to extract the fish out of the image as it is without any white background.</p>
<p>Thanks<br />
Harry</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Besterme</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-66027</link>
		<dc:creator>Besterme</dc:creator>
		<pubDate>Sat, 21 Jan 2012 03:29:47 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-66027</guid>
		<description>This is the good one: But please note that the Mask must be WHITE background and the mask area is BLACK.

-(CGImageRef) CopyImageAndAddAlphaChannel :(CGImageRef) sourceImage
{
	CGImageRef retVal = NULL;
	
	size_t width = CGImageGetWidth(sourceImage);
	size_t height = CGImageGetHeight(sourceImage);
	
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	
	CGContextRef offscreenContext = CGBitmapContextCreate(NULL, width, height, 
                                                          8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
	
	if (offscreenContext != NULL) {
		CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), sourceImage);
		
		retVal = CGBitmapContextCreateImage(offscreenContext);
		CGContextRelease(offscreenContext);
	}
	
	CGColorSpaceRelease(colorSpace);
	
	return retVal;
}
 
- (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
	CGImageRef maskRef = maskImage.CGImage;
	CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);
	
	CGImageRef sourceImage = [image CGImage];
	CGImageRef imageWithAlpha = sourceImage;
	//add alpha channel for images that don&#039;t have one (ie GIF, JPEG, etc...)
	//this however has a computational cost
	if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) { 
		imageWithAlpha = [self CopyImageAndAddAlphaChannel :sourceImage];
	}
	
	CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
	CGImageRelease(mask);
	
	//release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel
	if (sourceImage != imageWithAlpha) {
		CGImageRelease(imageWithAlpha);
	}
	
	UIImage* retImage = [UIImage imageWithCGImage:masked];
	CGImageRelease(masked);
	
	return retImage;
}

- (void)viewDidLoad
{
    [super viewDidLoad];        
    
    imgView.image = [self maskImage :[UIImage imageNamed:@&quot;input.png&quot;] withMask:[UIImage imageNamed:@&quot;mask.png&quot;]];
    
    
    
	// Do any additional setup after loading the view, typically from a nib.
}</description>
		<content:encoded><![CDATA[<p>This is the good one: But please note that the Mask must be WHITE background and the mask area is BLACK.</p>
<p>-(CGImageRef) CopyImageAndAddAlphaChannel :(CGImageRef) sourceImage<br />
{<br />
	CGImageRef retVal = NULL;</p>
<p>	size_t width = CGImageGetWidth(sourceImage);<br />
	size_t height = CGImageGetHeight(sourceImage);</p>
<p>	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();</p>
<p>	CGContextRef offscreenContext = CGBitmapContextCreate(NULL, width, height,<br />
                                                          8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);</p>
<p>	if (offscreenContext != NULL) {<br />
		CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), sourceImage);</p>
<p>		retVal = CGBitmapContextCreateImage(offscreenContext);<br />
		CGContextRelease(offscreenContext);<br />
	}</p>
<p>	CGColorSpaceRelease(colorSpace);</p>
<p>	return retVal;<br />
}</p>
<p>- (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage {<br />
	CGImageRef maskRef = maskImage.CGImage;<br />
	CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),<br />
                                        CGImageGetHeight(maskRef),<br />
                                        CGImageGetBitsPerComponent(maskRef),<br />
                                        CGImageGetBitsPerPixel(maskRef),<br />
                                        CGImageGetBytesPerRow(maskRef),<br />
                                        CGImageGetDataProvider(maskRef), NULL, false);</p>
<p>	CGImageRef sourceImage = [image CGImage];<br />
	CGImageRef imageWithAlpha = sourceImage;<br />
	//add alpha channel for images that don&#8217;t have one (ie GIF, JPEG, etc&#8230;)<br />
	//this however has a computational cost<br />
	if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) {<br />
		imageWithAlpha = [self CopyImageAndAddAlphaChannel :sourceImage];<br />
	}</p>
<p>	CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);<br />
	CGImageRelease(mask);</p>
<p>	//release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel<br />
	if (sourceImage != imageWithAlpha) {<br />
		CGImageRelease(imageWithAlpha);<br />
	}</p>
<p>	UIImage* retImage = [UIImage imageWithCGImage:masked];<br />
	CGImageRelease(masked);</p>
<p>	return retImage;<br />
}</p>
<p>- (void)viewDidLoad<br />
{<br />
    [super viewDidLoad];        </p>
<p>    imgView.image = [self maskImage :[UIImage imageNamed:@"input.png"] withMask:[UIImage imageNamed:@"mask.png"]];</p>
<p>	// Do any additional setup after loading the view, typically from a nib.<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chicio</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-65969</link>
		<dc:creator>chicio</dc:creator>
		<pubDate>Thu, 19 Jan 2012 13:19:19 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-65969</guid>
		<description>the script works and give an image with transparency out of the mask..it&#039;s correct and useful..thanks</description>
		<content:encoded><![CDATA[<p>the script works and give an image with transparency out of the mask..it&#8217;s correct and useful..thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harry</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-65934</link>
		<dc:creator>Harry</dc:creator>
		<pubDate>Wed, 18 Jan 2012 15:34:47 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-65934</guid>
		<description>Hi ,

I have an image with an white background. I want to extract that image portion of it. Can anybody help how to do this. I tried the above code it returning me an transparent image part of it.</description>
		<content:encoded><![CDATA[<p>Hi ,</p>
<p>I have an image with an white background. I want to extract that image portion of it. Can anybody help how to do this. I tried the above code it returning me an transparent image part of it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Van Du Tran</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-65856</link>
		<dc:creator>Van Du Tran</dc:creator>
		<pubDate>Tue, 17 Jan 2012 00:22:34 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-65856</guid>
		<description>Hi,

I implemented this, but the mask image (logo) is always taking up the whole original image size. I just want to add a logo at the right bottom corner, but the logo takes up the whole image. How do we fix this? Is there a way to position the mask?</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I implemented this, but the mask image (logo) is always taking up the whole original image size. I just want to add a logo at the right bottom corner, but the logo takes up the whole image. How do we fix this? Is there a way to position the mask?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nirav</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-65664</link>
		<dc:creator>Nirav</dc:creator>
		<pubDate>Thu, 12 Jan 2012 12:37:14 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-65664</guid>
		<description>Thanks for this valuable code. Can U suggest any ways to extract only masked portion from the image which is obtained from masking. I dont want transparent part to be present in the new image.</description>
		<content:encoded><![CDATA[<p>Thanks for this valuable code. Can U suggest any ways to extract only masked portion from the image which is obtained from masking. I dont want transparent part to be present in the new image.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tasnia</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-64288</link>
		<dc:creator>tasnia</dc:creator>
		<pubDate>Mon, 12 Dec 2011 03:35:21 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-64288</guid>
		<description>Thanks a lot for providing this important code.It&#039;s really needed..</description>
		<content:encoded><![CDATA[<p>Thanks a lot for providing this important code.It&#8217;s really needed..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ankit</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html#comment-58842</link>
		<dc:creator>Ankit</dc:creator>
		<pubDate>Thu, 25 Aug 2011 07:34:27 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1194#comment-58842</guid>
		<description>very simple solution to get rid of that black out area around


- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
	
	CGImageRef maskRef = maskImage.CGImage; 
	
	CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
										CGImageGetHeight(maskRef),
										CGImageGetBitsPerComponent(maskRef),
										CGImageGetBitsPerPixel(maskRef),
										CGImageGetBytesPerRow(maskRef),
										CGImageGetDataProvider(maskRef), NULL, false);
	
	CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
	UIImage *tempImage= [UIImage imageWithCGImage:masked];
	
	NSData *imageData=UIImagePNGRepresentation(tempImage);
	
	return ([UIImage imageWithData:imageData]);</description>
		<content:encoded><![CDATA[<p>very simple solution to get rid of that black out area around</p>
<p>- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {</p>
<p>	CGImageRef maskRef = maskImage.CGImage; </p>
<p>	CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),<br />
										CGImageGetHeight(maskRef),<br />
										CGImageGetBitsPerComponent(maskRef),<br />
										CGImageGetBitsPerPixel(maskRef),<br />
										CGImageGetBytesPerRow(maskRef),<br />
										CGImageGetDataProvider(maskRef), NULL, false);</p>
<p>	CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);<br />
	UIImage *tempImage= [UIImage imageWithCGImage:masked];</p>
<p>	NSData *imageData=UIImagePNGRepresentation(tempImage);</p>
<p>	return ([UIImage imageWithData:imageData]);</p>
]]></content:encoded>
	</item>
</channel>
</rss>

