2012-08-30 46 views
0

我試圖繞過uiimageview的頂部兩個角落,但下面的代碼不顯示只有正常圖像的圓角。我錯過了什麼嗎? THXUIimageView圓角頂部BezierPath不起作用?

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

回答

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

讓我知道如果它工作,或者如果您有更多的問題!

+0

謝謝!我會馬上嘗試,我會告訴你它是否有效 –

+0

它完美的作品,謝謝。不幸的是,圖片太大,桌面視圖滾動凍結太多了......我想我需要重新繪製上下文中的圖片......但是,謝謝!我認爲line line shapelayer.frame = imgView.layer !!!。bounds –

+0

這可以工作,但要小心 - mask和masksToBounds可能會導致性能下降,因此請確保您沒有大視圖或許多視圖。 – jjxtra