java
java

Reputation: 239

issue related UIimageView while making it round using cornerradius IOS

I have taken UIView in a XIB file. In this view there is an UIImageView and a same size of button. I want to make that view round and for that i am using cornerRadius. The size of the view is 92 * 92 that the reason why I am taking a cornerRadius of 46 to make it round. Below is my code and I am commenting the issue i am having in the code itself.

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

Upvotes: 3

Views: 1087

Answers (3)

freelancer
freelancer

Reputation: 1658

try below code

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


flag=0;



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



imageFrame = selectImage.frame;

Upvotes: 2

Apple_iOS0304
Apple_iOS0304

Reputation: 1102

Try this code:

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

Masks to bound is mandatory.. Also if you use a formula instead of direct values your code becomes more flexible. I hope it helps you!! good luck!!

Upvotes: 4

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Try to use this one..

_roundView.layer.masksToBounds = YES; 

Upvotes: 2

Related Questions