ios - UIImage 上的圆角

标签 ios objective-c iphone cocoa-touch image-manipulation

我正在尝试使用圆角在 iPhone 上绘制图像,例如联系人应用程序中的联系人图像。我有一些通常可以工作的代码,但它偶尔会在 UIImage 绘图例程中崩溃,并带有 EXEC_BAD_ACCESS - KERN_INVALID_ADDRESS。我认为这可能与 cropping question 有关几周前我问过,但我相信我正确设置了剪切路径。

这是我正在使用的代码 - 当它不崩溃时,结果看起来很好,任何想要获得类似外观的人都可以免费借用代码。

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

这是崩溃日志。每当我遇到这些崩溃之一时,它看起来都一样

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 及更高版本。每个基于 View 的对象都有一个关联的层。每一层都可以设置一个角半径,这将为您提供您想要的:

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]];

关于ios - UIImage 上的圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/205431/

相关文章:

iOS 在 UITableViewCell 中以编程方式设置 UIImageView 约束在滚动 tableview 时变得困惑

ios - 添加约束时 TableView 消失

javascript - 能否以编程方式填充 UIWebView Web 表单?

android - 我的网站无法在 iPhone 上正确缩放

ios - 适用于 iOS 的 Google Maps SDK 要求 GoogleMaps.bundle 成为“复制捆绑资源”下目标的一部分

ios - 成功登录 PFUser 后关闭 View 并路由到 Root View

ios - 如何将颜色数组中的文本颜色分配给标签?

ios - 如何获取网络更改通知(连接、断开连接、更改)

iphone - 调用 super ?

iphone - 关于button.tag = indexPath.row的帮助