【问题标题】:Iphone - Masking JPG imagesIphone - 屏蔽 JPG 图像
【发布时间】:2011-07-02 15:55:02
【问题描述】:

我正在编写一个拼图游戏,我需要对图像进行遮罩以创建拼图。

我正在使用来自在线服务器的图片,它们是 *.JPG。当我掩盖它们时,应该透明的区域是黑色的。

我可以通过编程方式添加 Alpha 通道吗?如果是,你能告诉我怎么做吗?

非常感谢, 安德烈

【问题讨论】:

    标签: iphone uiimage jpeg mask


    【解决方案1】:

    我找到了答案:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-17
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      相关资源
      最近更新 更多