【发布时间】:2012-11-20 08:50:29
【问题描述】:
通常当您在无效上下文中绘制时,您会看到类似于无效上下文 0x00 的内容,但情况并非如此,因为我看到了一个地址。我所有的核心图形代码都在视图的 drawRect 内。 有时,并非总是如此,我看到: “:CGContextFillRects:无效的上下文0xa88a500” 有时我的代码似乎有效,但其他代码却失败了。 我想画的是面具之类的东西。
如果您发现这里有什么问题,可以告诉我吗?:
UIGraphicsBeginImageContext(rect.size); CGContextRef 上下文 = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); CGContextFillRect(context, rect);
switch (shape) {
case SHAPE_ELIPSE:
//
CGContextAddEllipseInRect(context, maskBox);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillEllipseInRect(context, maskBox);
break;
case SHAPE_SQUARE:
//
CGContextAddRect(context, maskBox);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillRect(context, maskBox);
break;
default:
break;
}
CGContextStrokePath(context);
UIImage *backGroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef maskRef = backGroundImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
UIGraphicsBeginImageContext(rect.size);
CGContextRef contextBlack = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(contextBlack, [[UIColor blackColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *blackImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef masked = CGImageCreateWithMask([blackImage CGImage], mask);
CGImageRelease(mask);
[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithCGImage:masked]]];
CGImageRelease(masked);
非常感谢。
【问题讨论】:
标签: ios5 core-graphics quartz-graphics cgcontext