首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGImageCreateWithMaskingColors在苹果手表上不戴WKInterfaceImage面具

CGImageCreateWithMaskingColors在苹果手表上不戴WKInterfaceImage面具
EN

Stack Overflow用户
提问于 2015-04-24 23:59:44
回答 1查看 318关注 0票数 0

很久以前,我的iPhone应用程序中就有一段代码片段,试图用一些JPEG图像掩盖白色背景。今天,这条信息在iPhone上仍然有效,但在Apple上却没有任何遮掩。代码运行在预期的成功(非NULL)路径上,但不执行掩蔽。即使我将maskingColors数组更改为每个组件的范围0.0, 255.0,当显示在WKInterfaceImage中(与setImage:一起使用)时,也不会执行掩蔽(在iPhone上的相同更改完全掩盖了图像)。

存储在资产目录中的alpha通道的PNG图像似乎在WKInterfaceImage中的Apple上显示正常。

CGImageCreateWithMaskingColors对苹果手表不安全吗?

代码语言:javascript
复制
- (UIImage *)imageWithBackgroundRemovedWithImageData:(NSData *)imageData
{
    CGImageRef originalImage = [UIImage imageWithData:imageData].CGImage;

    /* Only attempt for RGB images */
    if (CGColorSpaceGetModel(CGImageGetColorSpace(originalImage)) != kCGColorSpaceModelRGB)
        return ([UIImage imageWithData:imageData]);

    /* Mask 10 shades of "white" */
    static const CGFloat maskingColors[] = {245.0, 255.0, 245.0, 255.0, 245.0, 255.0};
    CGImageRef transparentImageRef = CGImageCreateWithMaskingColors(originalImage, maskingColors);
    if (transparentImageRef == NULL)
        return ([UIImage imageWithData:imageData]);

    UIImage *transparentImage = [UIImage imageWithCGImage:transparentImageRef];
    CGImageRelease(transparentImageRef);
    return (transparentImage);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-29 18:09:30

这似乎是自iOS 7以来一直存在的问题。

使用该逻辑,我对代码进行了如下修改,以生成预期的结果:

代码语言:javascript
复制
UIGraphicsBeginImageContextWithOptions(transparentImage.size, NO, 1.0);
[transparentImage drawInRect:CGRectMake(0, 0, transparentImage.size.width, transparentImage.size.height)];
UIImage *anotherRendition = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return (anotherRendition);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29859401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档