2
votes

In my application i have used and image view to display images getting from database

in data base images are having different sizes,

for now i added image view with frame (10, 10, 300, 220).

I need to resize the frames of image view as per image size, just like Aspect Fit.

I know Aspect fit can able to resize but it do in changing width as per height ratio,

But i need to increase the height as per width ratio

i need to have width always 300, fixed but need to changes in height

For ex :

 if image size is 500x1000 i need to resize imageview as 300x600
 if image size is 400x600 i need to resize imageview as 300x450
2

2 Answers

3
votes

You can use the following code snippet which will meets your requirements

-(UIImage *)adjustImageSizeWhenCropping:(UIImage *)image
{

    float actualHeight = image.size.height;

    float actualWidth = image.size.width;



    float ratio=300/actualWidth;
    actualHeight = actualHeight*ratio;

    CGRect rect = CGRectMake(0.0, 0.0, 300, actualHeight);
    // UIGraphicsBeginImageContext(rect.size);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
    [image drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;


}
1
votes
resizedImage = [self imageWithImage:originalImage scaledToSize:CGSizeMake(45,45)]; 

self.imageView.image = resizedImage;

- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize 
{
    UIGraphicsBeginImageContext(newSize);

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

    UIGraphicsEndImageContext();

    return newImage;
}