问题描述
我正在尝试创建一个拼图游戏,我需要对UIImage进行遮罩以获得拼图.
I am trying to create a jigsaw puzzle and I need to mask the UIImages to obtain the puzzle pieces.
我不知道如何屏蔽JPG图片,因为据我了解它没有Alpha通道.谁能帮我这个?JPG位于在线服务器上,无法将其下载为PNG.
I don't understand how can I mask a JPG picture because as I understand it doesn't have an alpha channel. Can anyone help me with this?The JPGs are on an online server and there is no way to download them as PNG.
还有一件事,我在Apple文档中的任何地方都找不到此功能:"CopyImageAndAddAlphaChannel".它甚至存在.我在一些论坛上找到了一些参考资料,但毫无进展.
And one more thing, I can’t find this function anywhere on the Apple documentation:"CopyImageAndAddAlphaChannel". Does it even exist. I found a few references on some forums but nothing strait forward.
非常感谢,安德烈(Andrei)
Thanks a lot,Andrei
推荐答案
找到了答案.这是此功能,它适用于不带alpha通道的JPG和PNG(我已经测试过了:)):
Found the answer. Here is the function, it works for JPG and PNG without alpha channel (I have tested it :)):
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;
这篇关于iPhone-UIImage蒙版和CopyImageAndAddAlphaChannel函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!