下应该做什么你”重新过后。大部分代码是从How to set the opacity/alpha of a UIImage?我只是在将像素的颜色转换为黑色之前,为alpha值添加了一个测试。
// Create a pixel buffer in an easy to use format
CGImageRef imageRef = [[UIImage imageNamed:@"testImage"] CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UInt8 * m_PixelBuf = malloc(sizeof(UInt8) * height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(m_PixelBuf, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
//alter the alpha when the alpha of the source != 0
int length = height * width * 4;
for (int i=0; i<length; i+=4) {
if (m_PixelBuf[i+3] != 0) {
m_PixelBuf[i+3] = 255;
}
}
//create a new image
CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGImageRef newImgRef = CGBitmapContextCreateImage(ctx);
CGColorSpaceRelease(colorSpace);
CGContextRelease(ctx);
free(m_PixelBuf);
UIImage *finalImage = [UIImage imageWithCGImage:newImgRef];
CGImageRelease(newImgRef);
finalImage
现在将包含一个图像,其中不具有的0.0的α的所有像素具有为1
不错,只需要一个小的调整,使像素黑色: 'for(int i = 0; i
Halpo
2014-11-03 07:36:55
有了所有适当的推理,我不认为这种调整原始位图的像素缓冲区的技术是正确的方法。这假设alpha通道简单地反映了线路的不透明性。但是您渲染的路径可能具有消除锯齿的边缘;这种技术将不必要地在面具中引入锯齿。我建议不要像这样编辑像素缓冲区。相反,您想要使用1.0的alpha值重新渲染模型的原始路径。这样,您的面罩将享受边缘的抗锯齿性质,同时使主要行程路径完全不透明。 – Rob 2014-11-03 14:57:46