rexis.cn

一张相片 一段故事

Archive for the ‘mask’ tag

在图片上盖一层mask

without comments

- (UIImage*) createImage:(UIImage*)_image withMask:(UIImage*)_mask {
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGContextRef context = CGBitmapContextCreate(NULL,
						 _image.size.width,
						 _image.size.height,
						 8, 0,
						 colorSpace,
						kCGImageAlphaPremultipliedLast);
	CGColorSpaceRelease(colorSpace);
 
	CGRect rect = CGRectMake(0, 0, _image.size.width, _image.size.height);
	UIColor *bColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
	CGContextSetFillColorWithColor(context, [bColor CGColor]);                     //黑色是不透明,白色是透明
	CGContextFillRect(context, rect);
	CGContextClipToMask(context, rect, [_mask CGImage]);
	CGContextDrawImage(context, rect, [_image CGImage]);
 
	CGImageRef newImage = CGBitmapContextCreateImage(context);
	CGContextRelease(context);
 
	UIImage* _newImage = [[UIImage alloc] initWithCGImage:newImage];
	CGImageRelease(newImage);
 
	return [_newImage autorelease];
}

最先创建了一个灰度的颜色空间导致合成出来的mask颜色都是反的,黑色的显出白色,后来发现一篇例子是用RGB颜色空间来做再自己加上黑色填充后就得到了想要的效果,效果图:
lomo效果

Written by 北

五月 20th, 2010 at 11:59 下午

Posted in iphone开发

Tagged with , , ,