UIImageView image is stretching when i am using rounded corners.

Madhu

I am using UITableViewCell as UITableView Header. I am using the below code to make rounded corners for UIImageView.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if(section==0){

    static NSString *simpleTableIdentifier = @"ContactDetailsHeaderTableViewCell";

    ContactDetailsHeaderTableViewCell *cell = (ContactDetailsHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ContactDetailsHeaderTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

    ..................
    .................

UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];

        //rounded corners
        cell.profilePicImgView.clipsToBounds = YES;

        UIGraphicsBeginImageContextWithOptions(cell.profilePicImgView.bounds.size, NO, 1.0);

        // Add a clip before drawing anything, in the shape of an rounded rect
        [[UIBezierPath bezierPathWithRoundedRect:cell.profilePicImgView.bounds
                                    cornerRadius:60.0] addClip];
        // Draw your image
        [img drawInRect:cell.profilePicImgView.bounds];

        // Get the image, here setting the UIImageView image
        cell.profilePicImgView.image = UIGraphicsGetImageFromCurrentImageContext();

        // end drawing
        UIGraphicsEndImageContext();


.............
.............
}

Below is the output

enter image description here

The image inside the UIImageView is stretching, if i am not using the rounded corners then the image is looking properly. I have tried lot of code sample from StackOverflow but none of them are working.

Madhu

Figured out the issue, posting the answer so it may help others. Its not related to rounded corners code. The rounded corners code is working fine. In UIImagePickerController i have set allowsEditing = YES but in didFinishPickingMediaWithInfo i was using UIImagePickerControllerOriginalImage so later i changed it to UIImagePickerControllerEditedImage. Now its working fine. I am able to get the rounded corners image without any stretch.

-(void)capturePhoto{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                                    picker.delegate = self;
                                    picker.allowsEditing = YES;
                                    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

                                    [self presentViewController:picker animated:YES completion:NULL];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


/* earlier i have used UIImagePickerControllerOriginalImage in the place of UIImagePickerControllerEditedImage so it was not working properly*/
        UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

        ............
        ............





     [picker dismissViewControllerAnimated:YES completion:NULL];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android Image Background with Rounded Corners

Rounded Corners Image in Flutter

Creating a shadow for a UIImageView that has rounded corners?

Creating rounded corners using CSS

How to set UIImageView with rounded corners for aspect fit mode

IOS: create a UIImage or UIImageView with rounded corners

How to stop an image from stretching within a UIImageView?

Rounded corners on rectangular image using CSS only

How to remove black edge on UIImageView with rounded corners and a border width?

How to create an image with rounded corners in Java?

Diamond shape with rounded corners and background image

How to rounded the corners when I draw rectangle using UIBezierPath points

How can I clip and transform an image, adding rounded corners and perspective?

Drawing Rounded Corners Using UIBezierPath

tvOS: UIImageView with rounded corners without losing the focus animation

Rounded corners clipped in UIImageView

UIButton with background image, rounded corners and a shadow

Image with rounded corners and PreserveAspectFit in Qt

Image with rounded corners

When I make smaller screen, the image not stretching

Linearlayout with rounded corners and image resource background?

Set image with rounded corners into ImageView

Rounded image without resizing/cropping/stretching

Combine two UIImageView in to one image stretching

Corners of button not staying rounded when using constraints constraints

Rounded corners of an image

PIL - How can I add rounded corners to a pasted image?

Image with rounded bottom corners

Makes rounded corners on image using PHP GD