【发布时间】:2011-07-02 15:55:02
【问题描述】:
我正在编写一个拼图游戏,我需要对图像进行遮罩以创建拼图。
我正在使用来自在线服务器的图片,它们是 *.JPG。当我掩盖它们时,应该透明的区域是黑色的。
我可以通过编程方式添加 Alpha 通道吗?如果是,你能告诉我怎么做吗?
非常感谢, 安德烈
【问题讨论】:
我正在编写一个拼图游戏,我需要对图像进行遮罩以创建拼图。
我正在使用来自在线服务器的图片,它们是 *.JPG。当我掩盖它们时,应该透明的区域是黑色的。
我可以通过编程方式添加 Alpha 通道吗?如果是,你能告诉我怎么做吗?
非常感谢, 安德烈
【问题讨论】:
我找到了答案:
CGImageRef imageRef = self.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
// The bitsPerComponent and bitmapInfo values are hard-coded to prevent an "unsupported parameter combination" error
CGContextRef offscreenContext = CGBitmapContextCreate(NULL,
width,
height,
8,
0,
CGImageGetColorSpace(imageRef),
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
// Draw the image into the context and retrieve the new image, which will now have an alpha layer
CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), imageRef);
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(offscreenContext);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
// Clean up
CGContextRelease(offscreenContext);
CGImageRelease(imageRefWithAlpha);
return imageWithAlpha;
【讨论】: