Image ResizableImageWithCapInsets resizing Mode not stretching image uniformly - ios

I have an image view of size
CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 30)
I have an image sized 50 x 50 Px. It contains an drawing at the centre of it. I wish to stretch this image to fit the imageView. I achieve this by the following
viewImage.contentMode = UIViewContentModeScaleAspectFit;
Then it stretches the image in all directions and looks ugly. Then I used the resizeImageWithCapInsets as the following
[placeHoldImage resizableImageWithCapInsets:UIEdgeInsetsMake(1.0, 1.0, 40.0, 40.0) resizingMode:UIImageResizingModeStretch]
However this produces the following image
What I wish to change in this image is that the house symbol at the right bottom corner to be centred. This is the drawing of the image (50 x 50) which I dint want to get stretched.

Related

Why Imageview always stretched show image stretched iOS?

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.

Swift - Issues with setting background image in a UIScrollView

I have created a UIScrollView and added UILabels and UIButtons. I then have created a UIImageView and added it to the my UIScrollView. I have used the following code to set the UIImageView to the background of my UIScrollView.
let paperImageView = UIImageView(image: UIImage(named: "White Notebook Paper.png"))
paperImageView.frame = CGRect(x: -60, y: -60, width: UIScreen.main.bounds.width + 60, height: currentY + 60)
scrollView.addSubview(paperImageView)
scrollView.sendSubview(toBack: paperImageView)
As you can see in the following screenshot, by UIImageView is distorted.
The background image supposed to look like notebook paper. As you can see, the spacing of the lines becomes wider the lower you go in the UIScrollView. Thanks for taking the time to share your thoughts.
I forgot to change the content mode of the image view.
paperImageView.contentMode = .scaleAspectFill

What size should I have my navigationItem.titleView set to?

I've been trying to put an image in my titleView in the navigation bar. The image keeps coming out way too blurry and I can't seem to understand why for the life of me.
Right now my images are 28x14 for the #1x, 56x28 for the #2x, 84x42 #3x and the frame of the titleView is 50x50. Should I be sizing the #1x 50px, #2x 100px and the #3x at 150px? Essentially, does the width and height of the image rely on the frame?
I'm not entirely sure how to go about this, any help would be appreciated. I am trying to have the image work for all iPhone sizes, 5,6 and 6 plus.
Code is below:
let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
imageView.image = UIImage(named: "NavigationTitleLogo")
self.navigationItem.titleView = imageView
Addition
I'm also having the same issue with my MKAnnotationPin images.

UIImageView with fixed width: how to set its height to keep its aspect?

I have a view where I need to place an UIImageView, and I was told to place it inside a rectangle that takes the screen width and its height is smaller than width (fixed size). However, the images I've been given are more or less square jpeg images, so the idea is to fill the width of the rectangle that should contain the image and set the height of the image in a way that its aspect ratio is kept. Then, I should be able to let the user scroll the image vertically to see the complete image.
How could I do this?
Thanks in advance
EDIT: I need to do this for several images that have different sizes, but that should fit the same rectangular area size within the view
You can set imageview content mode.
imageView.contentMode = UIViewContentModeScaleAspectFit;
This will make sure that the image is displayed by keeping original aspect ratio.
Edit:
I think this is what you wanted:
UIImage *originalImage = [UIImage imageNamed:[picArr objectAtIndex:a]];
double width = originalImage.size.width;
double height = originalImage.size.height;
double apect = width/height;
double nHeight = 320.f/ apect;
self.img.frame = CGRectMake(0, 0, 320, nHeight);
self.img.center = self.view.center;
self.img.image = originalImage;
Hope this helps.. :)

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.

Resources