0

uiimageviewの上部の2つの角を丸めようとしていますが、次のコードでは、通常の画像だけが丸みを帯びた角を示していません。私は何かが足りないのですか?どうも

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

1 に答える 1

6

これは私のために働く

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

それが機能するかどうか、または他に質問がある場合はお知らせください!

于 2012-08-30T14:39:00.140 に答える