Shrink UIImage in bordered UIImageView - ios

I draw a border on my UIImageView...
[[albumImage layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[[albumImage layer] setBorderWidth:10.0];
But those extra 10 pixels are drawn inside the image view. Part of my image is cut off! I want to either:
Shrink the UIImage with a 0.96 factor so it fits inside the border.. or
Shrink the UIImageView down 10 pixels on each side in IB, then draw the border outside the image view
Using contentMode, transform, and contentScaleFactor haven't been the correct solution. The last two scale the entire image view (including the border). Content mode just changes how the image fits in the view (which has already been set, in my case)

I would add the image view into a container view and draw the border on the container view. If you're doing the container in XIB then you can set the size there. If you're doing it in code, set the image view frame as:
imageView.frame = CGRectInset(containerView.bounds, 10, 10);

Related

Upper and lower border around a UIImage

I have a UIImage inside a UITableViewCell.
What is the best way to have upper and lower borders around the UIImage?
[cell.backgroundImageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[cell.backgroundImageView.layer setBorderWidth: 2.0];
cell.backgroundImageView.layer.frame = CGRectMake(0, 0, cell.backgroundImageView.layer.frame.size.width + 10, cell.backgroundImageView.layer.frame.size.height);
Doesn't seem to work.
The border properties manage a border around the whole layer, not a partial one.
I'd go with a couple of UIViews (with their background color set as needed), pinned to the UIImageView using autolayout and pinned within the cell frame. In that way you don't risk drawing the borders outside the bounds of the cell, or having your other cell content mis-aligned to the image, since the image appears to be a certain size but it actually larger because you have been playing with the layers.
If performance is not a consideration you can add a UIView with 1px height above and below the image and set its background color.

Resizing UIImageView without scaling UIImage

I have a UIImageView that display a UIImage with the content mode set with UIContentModeScaleAspectFit. The imageview is as width as the screen. I want to resize the imageview without changing image position and scale. So, after resizing, the UIImage would be still virtually at the same position and still virtually as width as the screen, but not entirely visible because of the imageview resizing.
I try using UIContentModeLeft, while resizing from the right, to fix UIImage position, but my image wasn't at full resolution like when I'm using UIContentModeScaleAspectFit.
I'm clueless about this. I might need to use CGImage or CIImage, but I don't know where to start.
Set the image you want to display as the background of the view, instead of the source image.
You can use:
yourImageView.background = [UIColor colorWithPatternImage:[UIImage imageNamed:#"nameOfTheImageYouWantToDisplay"]];
I manage to solve my problem. Here's what I did :
Create a UIView in which I add a UIImageView subview
Set the imageview content mode to UIContentModeScaleAspectFit
Defining imageview frame to the screen with [[UIScreen mainScreen] bounds]
setClipsToBounds = YES to the UIView
Now, when resizing, the imageview is not scaling because its frame is define to be as big as the screen, setClipsToBounds: make sure I don't see part of the image that is outside of the UIView frame.

UIImageView doesn't resize itself to the UIImage it holds

I have a UIImageView with Auto-layout to the container margins. I set a UIImage to it in the ViewController. If I use UIViewContentModeScaleAspectFit then the image is centred in the middle of the screen as I wanted and everything looks great, but when I give the UIImageView a background color, I can see it still spreads all the way to the container margins, and doesn't get the image's proportions and dimensions. This is my code:
UIImage *passedImage = [UIImage imageNamed:self.photoTitle];
CGRect imageBounds = CGRectMake(0, 0, passedImage.size.width, passedImage.size.height);
[self.imageView setImage:passedImage];
self.imageView.bounds = imageBounds;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
I have tried everything to fix it and looked everywhere for answers, please help me, you are my only hope.
Instead of pinning the image view's edges to the container margins, pin its center X and center Y to the container's center X and center Y.
This gives the same visual result — the image is centered — but leaves the image view free to resize itself according to its contents.
UIImageView does not resize itself according to image size it renders. If you need to size it accordingly, you need to do it yourself. Matt's answer is reasonable, but you still need to update your image view size at some point. I'd suggest doing it in layoutSubviews or updateConstraints method of your view or in view controller's viewDidLayoutSubviews.

xcode UIView ScaleAspectFit for UITextfield setLeftView

I'd like to have an icon in my textfield and I'm using a the setLeftView attribute.
my code at the moment is
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 26, tb_teamA_playerA.bounds.size.height)];
leftView.contentMode = UIViewContentModeScaleAspectFit;
[leftView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed : #"person_26" ]]];
[tb_teamA_playerA setLeftView :leftView];
[tb_teamA_playerA setLeftViewMode: UITextFieldViewModeAlways];
that's the result.
the very left image is a separate UIImageView with the same image set and the scale set to ScaleAspectFit.
So I was expecting the two images to be the same size and scale but they are not?!
You are using UIViewContentModeScaleAspectFit, so the image will fit the size of the UITextField.
I think you should use UIViewContentModeScaleAspectFill, and if you have some problem with ths size and the two images still don't have the same size, you should resize you image.
The following is from the Doc
UIViewContentModeScaleToFill
Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.
UIViewContentModeScaleAspectFit
Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
UIViewContentModeScaleAspectFill
Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

Set UIImageView with size 10x10 as background for UIView

I have image with size 10x10 px and I need to set the image as background for UIView with the image copied horizontally and vertical, not stretched. Is it possible to do with ios?
you can do it like this:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bodyBG"]];
It copied horizontally and vertical in default.

Resources