代码如下:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CGContextRef context = ctx;
CGContextRetain(context);
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextSetLineCap(context, kCGLineCapButt);
CGContextSetLineWidth(context, 1);
CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.8);
// draw image
CGContextSaveGState(context);
CGAffineTransform flipVertical = CGAffineTransformMake(
1, 0, 0, -1, 0, layer.bounds.size.height
);
CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, layer.bounds, imageRef);
CGContextRestoreGState(context);
// draw borders
CGContextAddRect(context, layer.bounds);
CGContextDrawPath(context, kCGPathStroke);
CGFloat apertureSize = scaleFactor * (2 * samplingLevel - 1) - 1;
CGFloat x = self.bounds.size.width / 2 - apertureSize / 2;
CGFloat y = self.bounds.size.height / 2 - apertureSize / 2;
CGRect apertureRect = CGRectMake(x, y, apertureSize, apertureSize);
// draw the aperture
CGContextSaveGState(context);
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
CGContextSetShouldAntialias(context, NO);
CGContextStrokeRect(context, apertureRect);
CGContextRestoreGState(context);
CGContextRelease(context);
}
它崩溃了:
(lldb) thread backtrace
* thread #1: tid = 0x1c03, 0x37537a5c CoreGraphics`argb32_image_mark_rgb32 + 800, stop reason = EXC_BAD_ACCESS (code=1, address=0x7314b0)
frame #0: 0x37537a5c CoreGraphics`argb32_image_mark_rgb32 + 800
frame #1: 0x3750e400 CoreGraphics`argb32_image + 5948
frame #2: 0x313c5894 libRIP.A.dylib`ripl_Mark + 16
frame #3: 0x313ca68a libRIP.A.dylib`ripl_BltImage + 998
frame #4: 0x313c2468 libRIP.A.dylib`ripc_RenderImage + 180
frame #5: 0x313be300 libRIP.A.dylib`ripc_DrawImage + 584
frame #6: 0x3750a928 CoreGraphics`CGContextDelegateDrawImage + 48
frame #7: 0x3750a794 CoreGraphics`CGContextDrawImage + 292
frame #8: 0x00166e34 MyApp`-[MyView drawLayer:inContext:] + 380 at MyView.m:43
frame #9: 0x33f054e4 QuartzCore`-[CALayer drawInContext:] + 116
frame #10: 0x33f04b3e QuartzCore`CABackingStoreUpdate_ + 1782
frame #11: 0x33f04334 QuartzCore`CA::Layer::display_() + 956
frame #12: 0x33f03f60 QuartzCore`CA::Layer::display() + 128
有谁知道为什么?谢谢!我猜它是由 CGImage 或 CGContext 引起的,但它们在调试时都是非空的。我用来更新视图的图像是通过相机捕获生成的。