下應該做什麼你」重新過後。大部分代碼是從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