iOS中一行代码实现 UIView 镂空效果

2020-01-21 07:42:20王旭
image 图像。此时遮罩图的大小会被同步为 originView 的大小。

iOS,代码,UIView,镂空 2、将

UIImage 转换为只有 alpha 通道的 CGContextRef


CGImageRef originalMaskImage = [image CGImage];
float width = CGImageGetWidth(originalMaskImage);
float height = CGImageGetHeight(originalMaskImage);
  
int strideLength = ROUND_UP(width * 1, 4);
unsigned char * alphaData = calloc(strideLength * height, sizeof(unsigned char));
CGContextRef alphaOnlyContext = CGBitmapContextCreate(alphaData,
                           width,
                           height,
                           8,
                           strideLength,
                           NULL,
                           kCGImageAlphaOnly);
  
CGContextDrawImage(alphaOnlyContext, CGRectMake(0, 0, width, height), originalMaskImage);