我正在尝试使用圆角在 iPhone 上绘制图像,例如联系人应用程序中的联系人图像。我有通常可以工作的代码,但它偶尔会在 UIImage 绘图例程中崩溃,并带有EXEC_BAD_ACCESS
- KERN_INVALID_ADDRESS
。我认为这可能与我几周前提出的裁剪问题有关,但我相信我正确设置了裁剪路径。
这是我正在使用的代码——当它没有崩溃时,结果看起来很好,任何想要获得类似外观的人都可以免费借用代码。
- (UIImage *)borderedImageWithRect: (CGRect)dstRect radius:(CGFloat)radius {
UIImage *maskedImage = nil;
radius = MIN(radius, .5 * MIN(CGRectGetWidth(dstRect), CGRectGetHeight(dstRect)));
CGRect interiorRect = CGRectInset(dstRect, radius, radius);
UIGraphicsBeginImageContext(dstRect.size);
CGContextRef maskedContextRef = UIGraphicsGetCurrentContext();
CGContextSaveGState(maskedContextRef);
CGMutablePathRef borderPath = CGPathCreateMutable();
CGPathAddArc(borderPath, NULL, CGRectGetMinX(interiorRect), CGRectGetMinY(interiorRect), radius, PNDegreeToRadian(180), PNDegreeToRadian(270), NO);
CGPathAddArc(borderPath, NULL, CGRectGetMaxX(interiorRect), CGRectGetMinY(interiorRect), radius, PNDegreeToRadian(270.0), PNDegreeToRadian(360.0), NO);
CGPathAddArc(borderPath, NULL, CGRectGetMaxX(interiorRect), CGRectGetMaxY(interiorRect), radius, PNDegreeToRadian(0.0), PNDegreeToRadian(90.0), NO);
CGPathAddArc(borderPath, NULL, CGRectGetMinX(interiorRect), CGRectGetMaxY(interiorRect), radius, PNDegreeToRadian(90.0), PNDegreeToRadian(180.0), NO);
CGContextBeginPath(maskedContextRef);
CGContextAddPath(maskedContextRef, borderPath);
CGContextClosePath(maskedContextRef);
CGContextClip(maskedContextRef);
[self drawInRect: dstRect];
maskedImage = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(maskedContextRef);
UIGraphicsEndImageContext();
return maskedImage;
}
这是崩溃日志。每当我遇到这些崩溃之一时,它看起来都一样
异常类型:EXC_BAD_ACCESS (SIGSEGV) 异常代码:0x6e2e6181 处的 KERN_INVALID_ADDRESS 崩溃的线程:0 线程 0 崩溃: 0 com.apple.CoreGraphics 0x30fe56d8 CGGStateGetRenderingIntent + 4 1 libRIP.A.dylib 0x33c4a7d8 ripc_RenderImage + 104 2 libRIP.A.dylib 0x33c51868 ripc_DrawImage + 3860 3 com.apple.CoreGraphics 0x30fecad4 CGContextDelegateDrawImage + 80 4 com.apple.CoreGraphics 0x30feca40 CGContextDrawImage + 368 5 UIKit 0x30a6a708-[UIImage drawInRect:blendMode:alpha:] + 1460 6 UIKit 0x30a66904-[UIImage drawInRect:] + 72 7 MyApp 0x0003f8a8-[UIImage(PNAdditions)borderedImageWithRect:radius:] (UIImage+PNAdditions.m:187)