我正试图在iPhone上使用圆角绘制图像,这是一种在Contacts应用程序中显示联系人图像的方法.我有一些代码可以正常工作,但它偶尔会在EXEC_BAD_ACCESS-KERN_INVALID_ADDRESS的UIImage绘图 routine 中崩溃.我想这可能与几周前我问的cropping question个问题有关,但我相信我设置的剪辑路径是正确的.

下面是我正在使用的代码——当它没有崩溃时,结果看起来很好,任何想要获得类似外观的人都可以免费borrow 代码.

- (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;
}

这是坠机日志(log).每当我遇到这样的事故,情况都是一样的

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x6e2e6181
Crashed Thread:  0

Thread 0 Crashed:
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)

推荐答案

这里有一个在iPhone 3.0及更高版本中可用的更简单的方法.每个基于视图的对象都有一个关联的层.每一层都可以设置一个角半径,这将为您提供您想要的:

UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];

// You can even add a border
[l setBorderWidth:4.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];

Objective-c相关问答推荐

Objective-C:为什么[对象复制]的两个请求返回相同的结果?

iOS8:退出应用程序后不久出现蓝条正在使用您的位置

iOS - 如何预加载键盘?

Xcode 的任何 ReSharper 类似功能?

让 NSRunLoop 等待设置标志的最佳方法?

UISearchbar 键盘搜索按钮操作

自动属性合成 (@property) 和继承

Iphone,获取 NSArray 中的国家/地区列表

具有多个嵌入 segues 的 ContainerView

distanceFromLocation - 计算两点之间的距离

首次提交应用内购买以供审核

判断 NSNumber 是否为 NaN

交换 NSMutableArray 中的元素

检测 UITableView 滚动

通过情节提要加载时如何初始化视图?

iPhone如何在打字时获取UITextField的文本?

带有 UIImage 的 UIBarButtonItem 始终着色 iOS 7

UICollectionView:如何检测滚动何时停止

如何将按钮放在不会在 iOS 中随表格滚动的 UITableView 上

使用 [NSDate date] 获取当前日期和时间