iphone - UIimageView 圆顶角 BezierPath 不起作用?

标签 iphone objective-c ios quartz-graphics

我尝试将 uiimageview 的顶部两个角圆化,但以下代码不显示圆角,只显示普通图像。我错过了什么吗? 谢谢

        UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 328)];
imgView.backgroundColor = [UIColor clearColor];
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.image = image;
CAShapeLayer * shapeLayer = [CAShapeLayer layer];


shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 320, 328) byRoundingCorners:2 cornerRadii:CGSizeMake(20.0, 20.0)].CGPath;

    shapeLayer.frame = CGRectMake(0, 0, 320, 328);

    self.profilePictureView.layer.mask = shapeLayer.mask;

    self.profilePictureView.layer.masksToBounds = YES;
    [cell.contentView addSubview:self.profilePictureView];
    cell.contentView.backgroundColor = [UIColor clearColor];

最佳答案

这对我有用

- (void)viewDidLoad
    {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//Just to add contrast
[self.view setBackgroundColor:[UIColor blackColor]];

UIImage * image = [UIImage imageNamed:@"logo3w.png"];
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 328)];
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.image = image;
// Add the imageView to your view heirarchy, UITableViewCell or otherwise
[self.view addSubview:imgView];

CAShapeLayer * shapeLayer = [CAShapeLayer layer];
//Setting the background color of the masking shape layer to clear color is key
//otherwise it would mask everything 
shapeLayer.backgroundColor = [UIColor clearColor].CGColor;
    shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:imgView.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20.0, 20.0)].CGPath;

imgView.layer.masksToBounds = YES;
imgView.layer.mask = shapeLayer;
//Be sure to set the frame of the maskingLayer to be the bounds of the layer you want to   mask
shapeLayer.frame = imgView.layer.bounds;

//    self.profilePictureView had never been assigned imgView what we want to mask
//    self.profilePictureView.layer.mask = shapeLayer.mask;
//    self.profilePictureView.layer.masksToBounds = YES;

//    [cell.contentView addSubview:self.profilePictureView];
//    cell.contentView.backgroundColor = [UIColor clearColor];
}

让我知道它是否有效,或者如果您有更多问题!

关于iphone - UIimageView 圆顶角 BezierPath 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12197894/

相关文章:

objective-c - 在 switch 语句中使用变量作为 case 常量

ios - 后台任务和后台正确获取有什么区别?

iphone - iPhone崩溃日志在[UIApplication运行]中似乎崩溃了

iphone - 在 uipopovercontroller 中推送新的 tableViewController 会导致弹出框调整大小

iphone - 核心图 CPTBarPlotFieldBarLocation 和 CPTBarPlotFieldBarTip 的区别?

objective-c - 获取 iOS 支持的货币列表

ios - 将 Int 传递给 C 函数

iphone - 在 ARC 上显示 UIKeyboard 时内存泄漏

ios - 在 NSData 中存储 C 结构的 C 数组

ios - UIView 和 subview 委托(delegate)