Sudesh Kumar
Sudesh Kumar

Reputation: 569

resizing image with appropriate aspect ratio

I am working on new app, in which , i have different sizes of images. I have tried many times with different solution but could not find out the proper aspect ratio for images. I want to display images with a high quality too. There is some images have resolution 20*20 to 3456 * 4608 or it may be so high.

All images will be display without cropping ,but our app screen size is 300 * 370 pxls.

I was follow the following references :

1: Resize UIImage with aspect ratio?

2: UIViewContentModeScaleAspectFit

3: scale Image in an UIButton to AspectFit?

4:Resize UIImage with aspect ratio?

Upvotes: 0

Views: 716

Answers (2)

Peter
Peter

Reputation: 11

-- Please add you code example, otherwise its difficult to give any solution to your problem.

A solution found here is also How to scale a UIImageView proportionally?

imageView.contentMode = UIViewContentModeScaleAspectFit;

Upvotes: 1

Ugur Kumru
Ugur Kumru

Reputation: 986

The code that I use for resizing images to an edge 1000px at most is this :

const int maxPhotoWidthOrHeight = 1000;

- (UIImage*)resizeImageWithImage:(UIImage*)image
{
    CGFloat oldWidth = image.size.width;
    CGFloat oldHeight = image.size.height;
    NSLog(@"Old width %f , Old Height : %f ",oldWidth,oldHeight);

    if (oldHeight > maxPhotoWidthOrHeight || oldWidth > maxPhotoWidthOrHeight) {//resize if necessary
        CGFloat newHeight;
        CGFloat newWidth;
        if (oldHeight > oldWidth ) {
            newHeight = maxPhotoWidthOrHeight;
            newWidth = oldWidth * (newHeight/oldHeight);
        }
        else{
            newWidth = maxPhotoWidthOrHeight;
            newHeight = oldHeight * (newWidth/oldWidth);
        }
        CGSize newSize = CGSizeMake(newWidth, newHeight);
        UIGraphicsBeginImageContext( newSize );
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSLog(@"New width %f , New Height : %f ",newImage.size.width,newImage.size.height);

        return newImage;
    }
    else{//do not resize because both edges are less than 1000px
        return image;
    }

}

Upvotes: 1

Related Questions