2

I have a UIImageView with a size of (144,130) and the image size I am getting after capturing is (720,960).

I have resized image with

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Here new size is (144,130) which I have given because it is the imageviews size. Even though the image is getting stretched.

What should I make changes so that image does not get stretched?

EDIT-1

UIImage *imageCapture = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage *finalImage = [self imageByScalingAndCroppingForSize:(imageview.frame.size)];

as per the answer in this link

EDIT-2

Image captured has size : {720, 960}

imageview.frame.size : {144, 130}

newSize : {97.5, 130}

enter image description here

Community
  • 1
  • 1
Heena
  • 2,348
  • 3
  • 32
  • 58

2 Answers2

2

with resizing u have to maintain the aspect ratio. just match your view's aspect ratio with the actual image and there won't be any stretched image...

this could be useful for you

Community
  • 1
  • 1
Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
-1

You have set the Imageview Property

imageView.contentMode=UIViewContentModeScaleAspectFit;

and if you are using nib then set the imageview property in View section-> Mode -> Aspect Fit

There is no need to resize your image. It worked for me.

And though you want to resize the image you can have following image in UIImage category

- (UIImage *) scaleProportionalToSize: (CGSize)size
{
    float widthRatio = size.width/self.size.width;
    float heightRatio = size.height/self.size.height;

    if(widthRatio > heightRatio)
    {
        size=CGSizeMake(self.size.width*heightRatio,self.size.height*heightRatio);
    } else {
        size=CGSizeMake(self.size.width*widthRatio,self.size.height*widthRatio);
    }

    return [self scaledToSize:size];
}

- (UIImage *)scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(newSize);
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //[newImage autorelease];
    return newImage;
}

When you are resizing image to newSize you must take into account that, if you are resize width by suppose 10% then you must resize height by 10% only.