首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGImageMaskCreate:掩码位/组件无效: 16

CGImageMaskCreate:掩码位/组件无效: 16
EN

Stack Overflow用户
提问于 2017-09-25 23:52:55
回答 1查看 157关注 0票数 0

我正在更新一个旧的objc项目。我注意到创建图像蒙版在iOS 11和iPhone 7上不起作用

我得到"CGImageMaskCreate: invalid mask bits/component: 16“。警告。如果我将每个组件的位数减少到8(这似乎是最大值),它可以工作,但我绘制的渐变质量相当差。

下面是我的代码:

代码语言:javascript
复制
- (void)_background:(CGRect)rect
{
    // context for drawing
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGImageRef backgroundimage = CGBitmapContextCreateImage(context);
    CGContextClearRect(context, rect);
    //CGContextDrawImage(context, rect, backgroundimage); 

    // save state
    CGContextSaveGState(context);

    // flip the context (right-sideup)
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);     

    //colors/components/locations
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat black[4] = {0.0,0.0,0.0,BACKGROUND_ALPHA};
    CGFloat white[4] = {1.0,1.0,1.0,1.0};//clear

    CGFloat components[8] = {

        white[0],white[1],white[2],white[3],        
        black[0],black[1],black[2],black[3],
    };

    CGFloat colorLocations[2] = {0.25,0.5};

    // draw spotlights
    NSInteger spotlightCount = _positionArray.count;
    for (int i=0; i<spotlightCount; ++i)
    {
        // center and radius of spotlight
        CGPoint c = [[_positionArray objectAtIndex:i] CGPointValue];
        CGFloat radius = [[_radiusArray objectAtIndex:i] floatValue];


        CGContextSaveGState(context);

        //add gradient
        //create the gradient Ref
        CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorspace, components, colorLocations, 2);
        CGContextDrawRadialGradient(context, gradientRef, c, 0.0f, c, radius*2, 0);
        CGGradientRelease(gradientRef);

        CGContextRestoreGState(context);
    }

    CGColorSpaceRelease(colorspace);
    CGContextRestoreGState(context);

    //convert drawing to image for masking
    CGImageRef maskImage = CGBitmapContextCreateImage(context);

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskImage), 
                                        CGImageGetHeight(maskImage), 
                                        CGImageGetBitsPerComponent(maskImage), 
                                        CGImageGetBitsPerPixel(maskImage), 
                                        CGImageGetBytesPerRow(maskImage), 
                                        CGImageGetDataProvider(maskImage), NULL, NO);

    //      
    //------> CGImageMaskCreate: invalid mask bits/component: 16.
    //

    //mask the background image
    CGImageRef masked = CGImageCreateWithMask(backgroundimage, mask);
    CGImageRelease(backgroundimage);
    //remove the spotlight gradient now that we have it as image
    CGContextClearRect(context, rect);

    //draw the transparent background with the mask
    CGContextDrawImage(context, rect, masked);

    CGImageRelease(maskImage);
    CGImageRelease(mask);
    CGImageRelease(masked);
}

你有什么想法吗?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-06-08 02:38:16

CGImageMaskCreate的文档描述为“每个组件的图像掩码必须为1、2、4或8位”。

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

https://stackoverflow.com/questions/46409345

复制
相关文章

相似问题

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