我正在更新旧的objc项目。 我注意到创建一个图像蒙版并不适用于iOS 11& iPhone 7
我得到" CGImageMaskCreate:无效的掩码位/组件:16。"警告。 如果我将每个组件的比特减少到8(这似乎是最大的)它可以工作,但我绘制的渐变质量非常糟糕。
这是我的代码:
- (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);
}
你有什么想法吗?
感谢。
答案 0 :(得分:0)
CGImageMaskCreate
记录为“每个组件的图像掩码必须为1,2,4或8位。”