user2732294
user2732294

Reputation: 641

Objective c How to scale image view size (height) as per image size

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

Upvotes: 2

Views: 11496

Answers (2)

karthika
karthika

Reputation: 4091

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

Upvotes: 1

KAREEM MAHAMMED
KAREEM MAHAMMED

Reputation: 1695

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;


}

Upvotes: 3

Related Questions