UIImage should not stretched inside the UIImageView - ios

I am using the UIImageView in my xib. App is designed for multi device without auto layout. It means I am using autoresizing.What I want to do is, only my UIImageView should autoresize not my Image inside the UIImageView, but unfortunately my image also getting stretched with UIImageView. I have tried the different ways but could not get success. Changed the content mode also but didn't work for me.

Try this...
//set content mode
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//clear background color
self.imageView.backgroundColor=[UIColor clearColor];
UIViewContentMode
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,

Don't add the UIImage to the UIImageView whose size is going to change. Add it to another UIImageView and make this second UIImageView a subview of the first UIImageView.
UIImage *exampleImage = [UIImage imageWithContentsOfFile:filePath];
UIImageView *variableImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGFloat imageX = (variableImageView.bounds.size.width - exampleImage.size.width) / 2;
CGFLoat imageY = (variableImageView.bounds.size.height - exampleImage.size.height) / 2;
UIImageView *helperImageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, exampleImage.size.width, exampleImage.size.height)];
helperImageView.image = exampleImage;
helperImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:variableImageView];
[variableImageView addSubview:helperImageView];
I hope this helps.

Related

UIViewContentModeScaleAspectFit in UIImageView does not work correctly

I try to fit an Image into my UIImageView which is called imgBack
I define my UIImageView in the header as IBOutlet
IBOutlet UIImageView *imgBack;
in my viewDidLoad function I set the contentMode like this:
imgBack.contentMode = UIViewContentModeScaleAspectFit;
But after I load an image into imgBack like this:
imgBack.image = [UIImage imageWithContentsOfFile: imagePath];
it does not scale to fit into the imageview
This is the picture I load into imgBack:
image i try to load
And this is how it looks like in the view: image in UIImageView
As you see, the image does not fit into the View.
Does anyone know why?
If changing ImageView contentMode does not work for you, then you can try to resize the image to fit in Image View with help of following code
UIImage *originalImage = [UIImage imageWithContentsOfFile: imagePath];
imgBack.image = [self resizeImage: originalImage imageSize: imgBack.size];
and add this code to your ViewController
-(UIImage*)resizeImage:(UIImage *)image imageSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
//here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();
return newImage;
}
Attach the image to UIImageView first and then change the content mode
imgBack.image = [UIImage imageWithContentsOfFile: imagePath];
imgBack.contentMode = UIViewContentModeScaleAspectFill;
imgBack.clipsToBounds = YES;
below are the mode try and check
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,

How do I make a UIImageView not be wider than the view it's contained in?

I set the image of a UIImageView to an image that is 1024x1024, and as a result a lot of the image is not visible, especially width wise, and cut off the edges of the screen.
I tried using clipsToBounds:
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.clipsToBounds = YES;
[darkOverlayView addSubview:imageFromLink];
But it doesn't seem to do anything, and the image is still too big for the view.
You're going to want to change the contentMode property for the desired effect. UIViewContentModeScaleAspectFill is most likely what you'll want.
You have the set the image view's frame to the size you want. By default it will be the size of the image.
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = CGRectMake(0, 0, desiredWidth, desiredHeight);
[darkOverlayView addSubview:imageFromLink];
Also set the contentMode so the image is scaled properly in the smaller image view.
You should set the imageview's frame equal to the container's bounds.
UIImageView *imageFromLink = [[UIImageView alloc]initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = darkOverlayView.bounds;
[darkOverlayView addSubview:imageFromLink];

Make an UIImageView and its UIImage scale proportionally without extra padding

I have an UIView that contains a UIImageView. The UIImageViews works like the branding logo of the app. When I rotate the device, the containing UIView resizes itself to correspond to the landscape or portrait proportions of the screen.
What I'm trying to achieve is to have the UIImageView scaled accordingly, keeping proportions also on the left margin.
This is the actual code for the top white "banner":
UIView *topBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, height_topBanner)];
[topBanner setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin)];
[topBanner setBackgroundColor:[UIColor whiteColor]];
topBanner.autoresizesSubviews = YES;
// the logo
UIImage *topBanner_logo = [UIImage imageNamed:#"logo.png"];
float logoAspectRatio = topBanner_logo.size.width/topBanner_logo.size.height;
topBanner_logoView = [[UIImageView alloc] initWithFrame:CGRectMake(topBanner.frame.size.width/100*3, topBanner.frame.size.height/100*7, (topBanner.frame.size.height/100*86)*logoAspectRatio, topBanner.frame.size.height/100*86)];
[topBanner_logoView setImage:topBanner_logo];
topBanner_logoView.backgroundColor = [UIColor blueColor];
topBanner_logoView.contentMode = UIViewContentModeScaleAspectFit;
[topBanner_logoView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin)];
[topBanner addSubview:topBanner_logoView];
[self.view addSubview:topBanner];
This is my starting point: portrait iPad on startup:
This is what happens when I rotate it in landscape:
As you can see, the proportions of the UIImage are ok, but I'm getting extra borders (I set the background color of the UIImageView to highlight it) because the UIImageView stretches itself to follow the change of the size of its container, and the UIImage is fit into the UIImageView and put on its center.
The same - reversed - happens when I start the app directly in landscape mode:
Then I rotate it:
... and I get the logo with extra borders on top and bottom.
I do see that I can write a function to recalculate every size on each rotation change, but I'm asking to myself if is there a way to set the UIImageView and the UIImage to make it works without hacking the autorotate/resize procedures of iOS. It sounds so simple!
You can solve this by not using UIViewContentModeScaleAspectFit, and instead calculating the aspect ratio of the image and using that to explicitly the width or height based on the other (width or height).
e.g. I rotate to landscape, and so I want the height to be 80% of the view.
CGFloat w = logo.image.size.width;
CGFloat h = logo.image.size.height;
CGFloat a = w / h;
CGFloat h_use = self.view.height *0.8;
CGFloat w_use = h_use*a;
Furthermore, set the content mode to UIViewContentModeScaleAspectFill instead now that you've explicitly set the aspect ratio.
You have set the auto resizing mask to flexible height and width:
[topBanner_logoView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin)];
If you do not do that, the default is that the view will not chance size, and therefore, the image will not either.
I think it is because of topBanner_logoView.contentMode = UIViewContentModeScaleAspectFit;
Try topBanner_logoView.contentMode = UIViewContentModeCenter or topBanner_logoView.contentMode = UIViewContentModeLeft to prevent the UIImageView's image from resizing (and getting padding).
If the UIImageView is resizing, remove the autoresizing mask.

uitableview.backgroundView image dimension is compressing/distorting

I'm building an iphone app.
I have a tableview and i set a background image like so:
UIImageView * bg = [[UIImageView alloc] initWithImage:somebackgroundimage];
CGRect framebg = bg.frame;
framebg.origin.y = -64;
framebg.origin.x = 0;
framebg.size.width = 320;
framebg.size.height = 480;
[bg setFrame:framebg];
[_tblview setBackgroundView:bg];
no matter what framebg.size.width or framebg.size.height I set, the image always appears distorted or compressed with a size of maybe 320 width and 400 height. It is almost as if the app will forcefully resize the image to fit between the top navigation bar and my bottom tab bar.
How do I force tableview background image to be of 320px width by 480px height?
Also it seems the origin.y and origin.x aren't being respected either.
I know this question is a bit old, but I thought i would share what I was able to use to get around a similar issue (using setContentMode for the image view):
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[bg setContentMode:UIViewContentModeScaleAspectFit];
self.tableView.backgroundView = bg;
Hope this helps.

Resize UILabel correct on top of UIImageView

My problem is as follows:
I have on top of a UIImageView some labels. The image is a formular and the UILabels represents the entries.
If I rotate the device the image inside UIImageView gets resized with option "aspect fit".
How can I achieve that the UILabel is also resized and aligned correct?
The normal options for resizing aren't working as needed.
The UIImageView is zoomable with minimumZoomScale and maximumZoomScale set.
Thanks.
You can resize a UILabel proportionally using:
label.transform = CGAffineTransformMakeScale(0.5, 0.5);
You can reposition a UILabel relatively using:
label.frame = CGRectOffset(label.frame, deltaX, deltaY);
The rest is maths =)
So I managed to solve your problem. Full source code here.
Basically I removed your UIImageView and replaced it with a UIView. Then I added your UILabels within the UIView and removed any autosizing from the UIView and UILabels in interface builder. That way when the UIView gets resized any content in it will get resized/repositioned as well.
The next step was to change the class of the UIView to UIImageView in interface builder. Now from the source code I could set an image to it using imgView.image = [UIImage imageNamed:#"imagename.png"]; as well as I could set the mode using imgView.contentMode = UIViewContentModeScaleAspectFit;.
The transformation at rotation now happens with:
if (UIDeviceOrientationIsLandscape(deviceOrientation)){
// Play with the scale if you have any other images with different ratio.
float scale = 2.3f/3.0f;
// Transforming the UIImageView containing the UILabels which actually is a simple UIView
imgView.transform = CGAffineTransformMakeScale(scale, scale);
} else {
// Original size again
imgView.transform = CGAffineTransformMakeScale(1, 1);
}

Resources