0

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?

Community
  • 1
  • 1
Sudesh Kumar
  • 569
  • 3
  • 13
  • Can't you just get the ratio width/height of your screen and then apply that to your image that you have to display? – Nick Bull Mar 05 '12 at 14:26

2 Answers2

1

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

}
Ugur Kumru
  • 986
  • 6
  • 9
1

-- 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;
Community
  • 1
  • 1
Peter
  • 11
  • 1