I've got a UIImageView which has a height greater than its UIImage. It is scaling the image to it's width, but since it's height is greater, it is showing the rest of the UIImageView as transparent (what you would expect from UIViewContentModeScaleAspectFit, which is what the UIImageView's contentMode is set to)
However, it is making the top and bottom of the UIImage transparent, basically, positioning the image in the center of itself. How can I get it to position itself at the top? When I use UIViewContentModeTop, it makes the image too large, so that doesn't work for me. I want some sort of way to scale it just as it does with UIVIewContentModeScaleAspectFill but tell it to also position the scaled image at the top of itself. Anyone have any ideas on how to do that?
I would also be able to fix the issue by just trimming the UIImageView's height to be equal to what the UIImage is once it's been stretched to the boundaries of its superview, but that seems like a more complex solution.
try something like,
UIImage *img = [UIImage imageNamed#"yourImageName.jpeg"];
yourImageView.contentMode = UIViewContentModeScaleAspectFit;
yourImageView.clipsToBounds = YES;
[yourImageView setImage:img];
Related
In an app I've built, I had a table view showing an image to the left of each cell by running the following code inside of cellForRowAtIndexPath:
cell.imageView.image = [UIImage imageNamed:myImage];
But the image was too big, so I shrunk it:
cell.imageView.transform = CGAffineTransformMakeScale(0.3, 0.3);
This worked just fine in iOS 10. But once I upgraded to the newest Xcode with the iOS 11 SDK, the images got enormous. It turns out that that second line of code transforming the image view is now doing nothing: I can comment it out, change the 0.3's to something else, etc., and it doesn't make any difference. CGAffineTransformMakeScale still has documentation in the new Xcode, so I'm assuming it wasn't deprecated, but then why did this break with iOS 11, and how do I fix it? Any ideas? Thanks in advance! Please note, I'm using Objective-C.
Edit:
Just tried 3 changes to the code:
Change the second line to cell.imageView.transform = CGAffineTransformMakeScale(0.0000001, 0.0000001);. Nothing happens (i.e., the image views and the images inside them are still just as huge).
Change the second line to cell.imageView.transform = CGAffineTransformMakeScale(0, 0);. The image disappears from the image view, but the image view is still the same size, and you can tell because it still displaces the text in the cell and pushes it far to the right.
Remove the first line of code (no longer assigning an image to the imageview). The imageview disappears, and the text moves all the way to the left of the cell.
Perhaps this can help shed some light on what's going on?
So if you are trying to adjust the image size to fit the imageView you should actually use the imageView's contentMode property like so:
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
or in Swift for others
cell.imageView.contentMode = .scaleAspectFit
This keeps the dimensions of the image and fits the maximum size image onto the imageView without changing the dimensions
You could also try UIViewContentModeScaleAspectFit (or .scaleAspectFill in Swift) which basically fills the dimensions of the imageView entirely, but if the picture is wider or taller than the image view it crops what can't fit.
Here are all the contentModes directly from a Obj-C and Swift source files:
typedef NS_ENUM(NSInteger, 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,
};
public enum UIViewContentMode : Int {
case scaleToFill
case scaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
case scaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped.
case redraw // redraw on bounds change (calls -setNeedsDisplay)
case center // contents remain same size. positioned adjusted.
case top
case bottom
case left
case right
case topLeft
case topRight
case bottomLeft
case bottomRight
}
EDIT:
Since I see you're also interested in changing the dimensions of the imageView itself ("to leave more room for the text") what I would suggest is actually, either in storyboard or programmatically use AutoLayout to add your own imageView and have it sized and placed how you want it, instead of using the default imageView on a cell which is meant as a convenient/standardized tool.
If you are unfamiliar this I would google for an AutoLayout tutorial. Or maybe "using AutoLayout to create custom UITableViewCell"
If you dont actually want to create your own subclass you can try setting the `cell.imageView.frame = ..." somewhere to manually resize it to what you want, then setting its content mode to make sure the image fits nicely still.
See this question: How do I make UITableViewCell's ImageView a fixed size even when the image is smaller
Found an answer to my own question, with credit due to Paul's answer from this question: How to resize a cell.imageView in a TableView and apply tintColor in Swift
CGSize itemSize = CGSizeMake(50, 50);
UIGraphicsBeginImageContextWithOptions(itemSize, false, 0);
CGRect imageRect = CGRectMake(0, 0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I still don't know why the old CGAffineTransformMakeScale doesn't work anymore, but this gets the job done.
I am using an imageView and it contains different images, but some images are stretched. How do I display the images without any stretch? I have set the contentMode to aspect fit. However, the images are not in the size of the imageView, and it is in different sizes. I want to show the images as the same size without stretching.
imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];
imageview1.contentMode = UIViewContentModeScaleAspectFit;
imageview1.clipsToBounds = YES;
Please try setting content mode before setting frame like below
imageview1.contentMode = UIViewContentModeScaleAspectFit;
imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];
imageview1.clipsToBounds = YES;
If you want to display each image without stretching and of same size, than set contentMode property of UIImageView's instance to UIViewContentModeScaleAspectFill. For instance-
imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];
imageview1.contentMode = UIViewContentModeScaleAspectFill;
imageview1.clipsToBounds = YES;
UIViewContentModeScaleAspectFill, will fill the entire area of image view, keeping the aspect ratio intact. In the process of filling entire image view, either vertical or horizontal length will be fully covered and the filling will continue till the time other dimension is fully filled. In the process, your content(either across vertical or horizontal) will be visible outside the frame. To clip this extra content we have set clipsToBounds property of image view to YES.
UIViewContentModeScaleAspectFit, will fill the image view's area till the time any one length either vertical or horizontal is fully filled keeping the aspect ratio intact. Its useful, if you are not required to show each image as same size as the other direction (if vertical is fully filled than horizontal or vice versa), is not fully covered. Since this will show blank spaces in the direction which is not fully covered.
I'm going to use rounded corner box as 'container' of some UIComponents and, of course, it will requires multiple width and height.
my question is... is there any way to have just one PNG file with rounded corner but programatically stretch some area horizontally or vertically so it can be used in multiple UIView. if so, what UIComponent will be used to hold this image? is it UIImageView?
here's what I mean :
You can use below method to stretch imageview :
UIImage *img = [UIImage imageNamed:#"imageName"];
img = [img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
and then its img into imageview.
When i try to add image in imageView, it might be got stretch. My imageView size is fixed.
So, i add like as follow:
[imgView setContentMode:UIViewContentModeScaleAspectFit];
But if the image is sometimes cut off, small. So, image view got blank view above the image if image is small.
How can I take image as per image view without pixelated or stretch??
try [[yourimageview layer] setmasktobounds:yes];
UIViewContentModeCenter
Centers the content in the view’s bounds, keeping the proportions the
same.
UIViewContentModeScaleAspectFill
Just Check For maximum size of image and if its small then adjust size of imageview according to image.
here image is UIImage which is actually image to set on UIImageView
if(img.size.width < imgView.superview.frame.size.width ||
img.size.height < imgView.superview.frame.size.height)
{
[imgView setFrame:CGRectMake(0,0,img.size.width,img.size.height)];
[imgView setCenter:imgView.superview.center];
}
[imgView setContentMode:UIViewContentModeScaleAspectFit];
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.