我正在尝试创建一个拼图游戏,我需要掩盖 UIImages 以获得拼图。
我不明白如何屏蔽 JPG 图片,因为据我了解它没有 Alpha 通道。谁能帮我这个?JPG 位于在线服务器上,无法将它们下载为 PNG。
还有一件事,我在 Apple 文档的任何地方都找不到这个函数:“CopyImageAndAddAlphaChannel”。它甚至存在吗。我在一些论坛上找到了一些参考资料,但没有任何进展。
非常感谢,安德烈
找到了答案。这是函数,它适用于没有 alpha 通道的 JPG 和 PNG(我已经测试过了 :)):
CGImageRef imageRef = self.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
CGContextRef offscreenContext = CGBitmapContextCreate(NULL,
width,
height,
8,
0,
CGImageGetColorSpace(imageRef),
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), imageRef);
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(offscreenContext);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
CGContextRelease(offscreenContext);
CGImageRelease(imageRefWithAlpha);
return imageWithAlpha;