Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize UIImage and change the size of UIImageView

I have this UIImageView and I have the values of its max height and max width. What I want to achieve is that I want to take the image (with any aspect ratio and any resolution) and I want it to fit in the borders, so the picture does not exceed them, but it can shrink them as it wants. (marked red in the picture):

enter image description here

Right now the image fits the necessary size properly, but I have 2 worries: 1. The UIImageView is not equal the size of the resized image, thus leaving red background (and I don't want that) 2. If the image is smaller that the height of my UIImageView it is not resized to be smaller, it stays the same height.

Here's my code and I know its wrong:

UIImage *actualImage = [attachmentsArray lastObject]; UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.5, 245, 134)]; attachmentImageNew.image = actualImage; attachmentImageNew.backgroundColor = [UIColor redColor]; attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit; 

So how do I dynamically change the size not only of the UIImageView.image, but of the whole UIImageView, thus making its size totally adjustable to its content. Any help would be much appreciated, thanks!

like image 310
Sergey Grischyov Avatar asked Feb 01 '13 15:02

Sergey Grischyov


People also ask

How do I change the size of my UIImageView?

The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options. I would NOT retain the result here. newImage will be autoreleased already, and it should up to the method that called this to retain it or not.

How do I resize the UIImage to reduce upload image size?

It reduces the image size to less than 100kb. It works proportionally! extension UIImage { class func scaleImageWithDivisor(img: UIImage, divisor: CGFloat) -> UIImage { let size = CGSize(width: img. size.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


Video Answer


2 Answers

When you get the width and height of a resized image Get width of a resized image after UIViewContentModeScaleAspectFit, you can resize your imageView:

imageView.frame = CGRectMake(0, 0, resizedWidth, resizedHeight); imageView.center = imageView.superview.center; 

I haven't checked if it works, but I think all should be OK

like image 155
Mikhail Avatar answered Sep 18 '22 18:09

Mikhail


- (UIImage *)image:(UIImage*)originalImage scaledToSize:(CGSize)size {     //avoid redundant drawing     if (CGSizeEqualToSize(originalImage.size, size))     {         return originalImage;     }      //create drawing context     UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);      //draw     [originalImage drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];      //capture resultant image     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();      //return image     return image; } 
like image 31
Rajneesh071 Avatar answered Sep 19 '22 18:09

Rajneesh071