开发者

How to mask an view with an black-white image?

开发者 https://www.devze.com 2022-12-25 11:58 出处:网络
I\'ve heard that it\'s possible to mask views with black/white images, where black means fully transparent and white means view is visible. The big difference to clipsToBounds is that the view could b

I've heard that it's possible to mask views with black/white images, where black means fully transparent and white means view is visible. The big difference to clipsToBounds is that the view could be clipped in funny shapes like circl开发者_如何学编程es or stars. How could I do that?


CGImageRef maskRef = <some cgimage>; 
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), CGImageGetHeight(maskRef), CGImageGetBitsPerComponent(maskRef), CGImageGetBitsPerPixel(maskRef), CGImageGetBytesPerRow(maskRef), CGImageGetDataProvider(maskRef), NULL, YES);
CGContextClipToMask(context, maskRect, mask);
CGImageRelease(mask);


Look at a function

void CGContextClipToMask (
   CGContextRef c,
   CGRect rect,
   CGImageRef mask
);

If I understood correctly it does exactly what you want.


 CGImageRef maskImage = <Black-White Image>
 const CGFloat components[6] = {0,0,0,0,0,0}; // mask black color
 CGImageRef mask = CGImageCreateWithMaskingColors(maskImage, components);
 CGContextClipToMask(ctx, mask)
// draw images will be masked
0

精彩评论

暂无评论...
验证码 换一张
取 消