2016-04-25 8 views
0

円の半径でUIImageViewを作成する必要があります。このtaksのコードブロックを使用しています。 iOSの5で、完璧な作業が、他のデバイス、それ形状のテストが変更されたときに、それはなぜ起こるか私にはわからないuiimageviewサークルを作成するには?

-(void)viewWillAppear:(BOOL)animated { 

    [self performSelector:@selector(setStyleCircleForImage:) withObject:_imageview afterDelay:0]; 

    [super viewWillAppear:YES]; 
} 

-(void) setStyleCircleForImage:(UIImageView *)imgView { 

    imgView.layer.cornerRadius = _imageview.frame.size.height/2.0; 
    imgView.clipsToBounds = YES; 
} 

その。助けてください。

+1

2つのルール 1.コンポーネントの幅と高さは同じでなければなりません。 2.あなたのケースでは、フレームが更新されないかもしれません。動作するはずの 'viewWillLayoutSubviews'を入れてみてください。 – iphonic

+0

ok sir私はそれを実装しようとしました –

+0

imgView.layer.cornerRadius = imgView.frame.size.width/2.0;私はその仕事uiimageviewの幅と高さが同じにするが、他のデバイス –

答えて

1

良い

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self makeCircleImage: _imageview]; 
} 

    -(void)makeCircleImage:(UIImageView *)img{ 
     CGFloat img_width = img.bounds.size.width; 
     UIGraphicsBeginImageContextWithOptions(CGSizeMake(img_width,img_width), NO, 0); 
     CGContextRef c = UIGraphicsGetCurrentContext(); 
     CGContextSetFillColorWithColor(c, [UIColor blackColor].CGColor); 
     CGContextFillEllipseInRect(c, CGRectMake(0,0,img_width,img_width)); 
     UIImage* maskim = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     CALayer* mask = [CALayer new]; 
     mask.frame = CGRectMake(0,0,img_width,img_width); 
     mask.contents = (id)maskim.CGImage; 

     img.layer.mask = mask; 
    } 
+0

感謝の先生は、ただ私は私が私のイメージ名この方法の間で相関するか、それ –

+0

はいSIRをあなたのソリューションを実装しようとした –

+0

に取り組んでいますをmin待つのか? –

0

その後、使用すること、これが

yourImageView.layer.cornerRadius = yourImageView.frame.size.width/2; 
yourImageView.layer.masksToBounds = YES; 
+0

私はこのコードをviewdidloadメソッドに入れるべきですか? –

+0

はい。あなたはviewDidLoadで追加する必要があります。なぜあなたはSelectrorを使用していますか?あなたは 'masksToBoundsを'使用する必要が@sandeeptomar – Vvk

+0

は '' clipsToBounds – Vvk

0

まずUIImageViewためequal widthequal heightを与えるこの関数のようにサークルを作るためにマスクを使用してみてください、それ

imgView.layer.cornerRadius = imgView.frame.size.height/2.0; 

imgView.layer.masksToBounds = YES; 
+0

私はEI Captainの回答で問題を解決します。サポートしてくれてありがとう –

関連する問題