UIImageView Scale to fill does not clip subviews - ios

I am trying to add some images to my UIImageView. Everything works fine if the png has the same size as the ImageView.
But when I insert a bigger image, it is not discarded.
This is my code:
#IBOutlet weak var PictureView: UIImageView!
func loadSomePicture() {
var examplePicture = UIImage(named: "Block.png")
PictureView.image = exampleProfilePicture
PictureView.layer.cornerRadius = 50
PictureView.clipsToBounds = true
Althoug the clipsToBounds property is set true and the View Mode is Scale to fill, the image stays in its original size.
Thanks for Your help!

check Clip Subviews in Drawing options

I believe the problem is occurring because you are setting the contentMode to "Aspect Fill", instead of "Scale To Fill". Can you maybe explain a little more what you want the ImageView and Image to end up as? "Aspect Fill" will always try and keep the image's aspect ratio while filling it to the ImageView's size dimensions.

Related

how to remove white space in circle UIImageView?

I wanted to circle my UIImageView and added this code :
profileImage.layer.cornerRadius = profileImage.frame.size.height/2
profileImage.clipsToBounds = true
and it work perfectly, but when images are horizontal, I get this picture:
as you can see, there is white space at the bottom and top of my circle image view. but what I really wanted was a circle filled with my image!
I've tried changing "content Mode" from attribute inspector, but I didn't get any answer! how can I fix this issue?
You have already set the clipsToBound property. So just update the contentMode.
I you don't want to distort image's scale:
profileImage.contentMode = .scaleAspectFill
If image's scale does not matter, you also can use:
profileImage.contentMode = .scaleToFill
Follow the link for more details: https://useyourloaf.com/blog/stretching-redrawing-and-positioning-with-contentmode/
Use below code:-
profileImage.clipsToBounds = true
profileImage.contentMode = .scaleAspectFill
Set the contentMode of your UIImageView to scaleAspectFill
profileImage.contentMode = .scaleAspectFill
Keep in mind that using this contentMode option some portion of the content may be clipped to fill the view’s bounds.
You can set the Different Content Mode as per you choice from StoryBoard:
You can select
Scale to Fill
Aspect Fill
or any other content mode by using trial and Error method which suits you
Hope it Helps.
If I choose Aspect Fit, I'm getting blank space in top and bottom of the imageview.
Can I trim that space anyhow?
Is there any programatically constraints I can set to re-scale imageview according to actual image size?

AutoLayout: UIImageView and Aspect Fit content mode

I'm trying to place an UIImageView in the parent view's center. Image must keep its original aspect ratio and also it shouldn't exceed parent's bounds. So landscape images should be limited by parent's width and portrait images should occupy as much vertical space as needed keeping original ratio.
Sounds like quite a simple task for AutoLayout, right? Here's my setup for UIImageView:
center vertically in parent
center horizontally in parent
imageView.width <= superview.width
imageView.height <= superview.height
I also set contentMode to Aspect Fit. Everything works really great for small images which are smaller than device screen but for some reason my UIImageView takes more space than its underlying UIImage for large images (notice the green background - imageView.backgroundColor = [UIColor greenColor]).
Why is this happening? I am not an AutoLayout expert by my constraints look reasonable to me. If I use smaller images like 200x400 then UIImageView takes exactly 200x400 points on the screen with no extra green areas.
I'd like to add borders and rounded corners which obviously won't work properly in this case.
This is happenning because you've set width and height constraints as <=.
For small images imageView's frame is calculated without any issues and all constraints are satisfied. But when a big image is set, imageView's size is going to be set so that the image fits, but at the same it is limited by the size of the superview (screen size).
If you know aspect ratio for sure, you can set it as a constraint and
you won't see any green background.
Otherwise just leave your
constraints as they are and set background color to clear color. This
way any unoccupied zones will be transparent and any image you set
will take maximum space in at least one dimension.
Edit:
Probably not the best solution, kind of oldschool. You can add strict width and height constraints and calculate them manually using image's aspectRatio:
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
#IBOutlet weak var widthConstraint: NSLayoutConstraint!
func setImage(image: UIImage) {
imageView.image = image
let screenSize = UIScreen.main.bounds.size
let imageAspectRatio = image.size.width / image.size.height
let screenAspectRatio = screenSize.width / screenSize.height
if imageAspectRatio > screenAspectRatio {
widthConstraint.constant = min(image.size.width, screenSize.width)
heightConstraint.constant = widthConstraint.constant / imageAspectRatio
}
else {
heightConstraint.constant = min(image.size.height, screenSize.height)
widthConstraint.constant = heightConstraint.constant * imageAspectRatio
}
view.layoutIfNeeded()
}
Try to check "Clip to Bounds" for imageView in Interface Builder
Clip to Bounds Image View
and you must have a well-defined height and width, not "<="

Resizing UIImageView frame after scaleAspectFit

I created a UIImageView in IB with a Content Mode of Aspect Fit. I'm then loading a large image to that view from a URL.
#IBOutlet weak var pictureView: UIImageView!
NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, _, error) -> Void in
guard let data = data where error == nil,
let image = UIImage(data: data)
else {
print(error?.localizedDescription)
return
}
pictureView.image = image
pictureView.clipsToBounds = true
setNeedsLayout()
layoutIfNeeded()
}).resume()
The image loads in the the UIImageView, and Aspect Fit resizes the image correctly to display it all in the frame, however the frame of the UIImageView is set to the size of the original large image. I have this huge frame wrapping this smaller resized image. I need the UIImageView frame to tightly wrap the Aspect Fit UIImage, throwing off all my other formatting.
Do I need to somehow calculate a new frame size manually for the UIImageView, or am I missing a setting somewhere?
EDIT:
I've also tried the following to resize the UIImageView frame around the image:
pictureView.frame = AVMakeRectWithAspectRatioInsideRect(picture.size, pictureView.frame)
and...
pictureView.frame = CGRectMake(self.pictureView.frame.origin.x, self.pictureView.frame.origin.y, pictureImage.size.width, pictureImage.size.height)
And I've also tried playing with the compression resistance settings.
Well there a few things that are involved.
The dependencies :
1) The image size. If the image height/width ratio is not greater than or equal to ImageView height/width ratio, then there will be black space shown, as there will be less ratio to match to the ImageView.
2) I am unsure of the constrains of your imageView, since I can see it is an #IBOutlet.
I am confused on your sentence "however the frame of the UIImageView is set to the size of the original large image. I have this huge frame wrapping this smaller resized image."
I believe what you are wanting to do is set the image into the imageView, but it seems that you are setting the UIImageView into the image size then the image into UIImageView, which makes it really confusing to debug. Could you explain more detailed exactly what you are wanting to happen?
EDIT
There is an ImageView with a frame -- this frame will NEVER be different.
The contentMode will change how the image is displayed on the imageView
The UIViewContentModeScaleAspectFit and UIViewContentModeScaleAspectFill modes scale the image to fit or fill the space while maintaining the image’s original aspect ratio.
The UIViewContentModeScaleToFill value scales the image without regard to the original aspect ratio, which can cause the image to appear distorted.
UIViewContentModeScaleToFill
The option to scale the content to fit the size of itself by changing the aspect ratio of the content if necessary.
*(Wouldn't recommend for most cases)
UIViewContentModeScaleAspectFit
The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
*(This one guarantees that the WHOLE IMAGE fits on the screen. So if the image is smaller ratio than the screen, then there will be black on some edges, either top/bottom or left/right)
UIViewContentModeScaleAspectFill
The option to scale the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.
*(This one guarantees that the WHOLE IMAGEVIEW is taken)

How to display UIImage without having any stretching but all be the same height?

I am displaying images in a UITableViewController inside a cell, currently to show the full image I am setting the UIImageView as aspectFit but it doesn't fill up the entire UIImageView.
Is there a way, where I can show the full image and not distort it like the other contentView modes do?
Hope this will help :)
YourImageview.contentMode = UIViewContentModeScaleAspectFill;
YourImageview.clipsToBounds = true
You should use as below :
Youreimageview.contentMode = UIViewContentModeScaleAspectFit;
This is from the This apple Document:
UIViewContentModeScaleAspectFit
The option to scale the content to fit the size of the view by
maintaining the aspect ratio. Any remaining area of the view’s bounds
is transparent.
For the Image showing in the Tableviewcell. You need to do customization of the UItableviewCell, and then you need to put imageview in that. That imageview must have this content mode to set image.

How to fill the background image in a UIView without streching it

I'm trying to set the background image of a UIView with animated .gif's that I'm pulling form Giphy.
The problem I'm having is that the UIView is stretching the image. I want to fill the background so the height is 100% and have it centered. So left and right of the .gif would be cut off - but the image would be centered on the screen and not stretched.
Here's a screenshot of what it looks like now.
You can see it's fills height correct but it's shrinking the width of the image to match the UIView dimensions making it look stretched.
Here is the code I have now:
override func viewDidAppear(animated: Bool) {
self.giphyBackground(Title: "dancing") { [weak self] (gifUrl) -> Void in
let gifView = FLAnimatedImageView(frame: self!.giphy.frame)
gifView.animatedImage = FLAnimatedImage(animatedGIFData: NSData(contentsOfURL: NSURL(string: gifUrl)!)!)
self?.view.insertSubview(gifView, aboveSubview: self!.giphy)
}
}
Any ideas
I think you should set the content mode of gifView to Aspect Fit, doing so will not stretch the image and will fully fill at-least one of the length either horizontally/ vertically.
Try setting
gifView.contentMode = UIViewContentMode.ScaleAspectFit
Also you can use scale AspectFill on gifView as
gifView.contentMode = UIViewContentMode.ScaleAspectFill
ScaleAspectFill will fill the entire gifView's frame but will also maintain the aspect ratio, doing so your image won't look stretched but it could happen that content either horizontally or vertically will go outside the frame(which you can clip).
You are definitely looking for the view's contentMode ScaleAspectFit. Simply add a line gifView.contentMode=UIViewContentMode.ScaleAspectFit

Resources