3

我在 XIB 文件中使用了 UIView。在这个视图中有一个 UIImageView 和一个相同大小的按钮。我想让那个视图变成圆形,为此我正在使用cornerRadius。视图的大小为 92 * 92,这就是我采用 46 的cornerRadius 使其变圆的原因。下面是我的代码,我正在评论我在代码本身中遇到的问题。

- (void)viewDidLoad
{   
    [super viewDidLoad];
    _roundView.layer.cornerRadius = 46;
    flag=0;
    selectImage.layer.cornerRadius = 46;
    NSLog(@"%f",_roundView.frame.size.height);
    NSLog(@"%f",_roundView.frame.size.width);

    imageFrame = selectImage.frame;
    // when first view appear its perfectly round 
    // this is how i pick image from gallery 


    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {
    self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
        UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
        UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
        self.imagee = shrunkenImage;

        NSLog(@"%f",_roundView.frame.size.height); nslog = 92
        NSLog(@"%f",_roundView.frame.size.width);   nslog = 92
        NSLog(@"%f",_roundView.layer.cornerRadius); nslog = 46

        NSLog(@"%f",selectImage.frame.size.height);   nslog = 92
        NSLog(@"%f",selectImage.frame.size.width);   nslog = 92
        NSLog(@"%f",selectImage.layer.cornerRadius); nslog = 46

        selectImage.image = imagee;
        selectImage.layer.cornerRadius = 46;

        // now the issue is Image gets selected but it become rectangular 92 * 92 
    }

    [picker dismissModalViewControllerAnimated:YES];
}
4

3 回答 3

4

试试这个代码:

_roundView.layer.cornerRadius = _roundView.frame.size.height / 2;
_roundView.layer.masksToBounds = YES;

绑定掩码是强制性的。此外,如果您使用公式而不是直接值,您的代码将变得更加灵活。希望对你有帮助!!祝你好运!!

于 2013-07-10T05:54:18.207 回答
2

试试下面的代码

roundView.layer.cornerRadius = 46;
_roundView.layer.masksToBounds = YES;


flag=0;



selectImage.layer.cornerRadius = 46;
selectImage.layer.masksToBounds = YES;



imageFrame = selectImage.frame;
于 2013-07-10T05:54:38.857 回答
2

试试用这个..

_roundView.layer.masksToBounds = YES; 
于 2013-07-10T05:49:27.917 回答