I'm having problem in iOS simulator, the image is higher than it should. See the picture.
The correct thing.
Size of images:
#1x = 750x120
#2x = 1500x240
#3x = 2250x360
did not work, I did so:
divAcoes.contentMode = .ScaleAspectFill
divAcoes.backgroundColor = UIColor(patternImage: UIImage(named: "divInfo")!)
You need to provide the proper contentMode for the UIImageView.
That can be done in your Storyboard/XIB with Scale Aspect Fill or in code
imageView.contentMode = .ScaleAspectFill
You can always inspect your interface in runtime by clicking the "Debug View Hierarchy" button in Xcode while running the app.
It's either you have UIImageView's Mode set wrong or its size/position is incorrect.
Related
I would like to ask how to scale an image to fill a UIImageView but still keep its aspect ratio, just like the centerCrop scale type of Android ImageView does.
You should use .scaleAspectFill. In swift 4.2:
myImageView.contentMode = .scaleAspectFill
myImageView.clipsToBounds = true // If you want to crop
Following are the main content modes of UIImageView:
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
the one to fill the whole imageview is 'ScaleAspectFill' plus you can also use 'clipsToBound' property as true so that it will crop the extra image which is out of imageview.
You can set image view contentMode to UIView.ContentMode.scaleAspectFill. It is equivalent of Android scaleType centerCrop
I need an image in two different sizes. 100px and 50px
I exported it with a height of 100px
While using the scaleAspectFit contentMode the smaller imageView gets pixelated.
According to this answer the solution would be to scale the image before importing it into the project. Since I need 2 different sizes I would prefer an alternative where I could only include the higher size and scale it down for the smaller imageView in order to keep the project size smaller. Is there any solution to achieve non pixelated images?
let templateImage = UIImage(named: exercise.imageName)?.withRenderingMode(.alwaysTemplate)
imageView.image = templateImage
imageView.tintColor = UIColor.blue
imageView.contentMode = .scaleAspectFit
I have a UIImageView whose user interaction is true and to which I have given a tap gesture recognizer, whose action handler is as follows:
#IBAction func tap(_ sender:UITapGestureRecognizer) {
let iv = sender.view as! UIImageView
let im = iv.image!
let im2 = UIGraphicsImageRenderer(size:im.size).image { _ in
UIColor.red.setFill()
UIBezierPath(rect: CGRect(origin:.zero, size:im.size)).fill()
}
iv.image = im2
}
I expect the image displayed, when I tap the image view, to be replaced by a solid red image. This works fine on my High Sierra machine running Xcode 9.4. But on my Sierra MacBook running Xcode 9.2, nothing visibly happens.
It's weird. By pausing in the debugger, I can see that the new image is being constructed correctly:
The image is being replaced, but the image view isn't being redrawn. Adding calls like setNeedsDisplay does nothing.
Moreover, if I then proceed to replace the image view's image with a different image, I see the red image!
iv.image = im2
delay(0.5) {
iv.image = im // causes im2 to appear!
}
Some sort of behind-the-scenes caching is evidently causing the image view to get behind in its display by one image.
Can anyone shed light on this? It's presumably a bug in iOS itself, and perhaps in 9.2 specifically; how would one work around it? (Obviously one could substitute another image view wholesale, but that wouldn't tell us what's going on with the caching.)
This seems to be a workaround:
iv.image = im2
delay(0.05) {
iv.image = nil
iv.image = im2
}
But what a horror... Omitting any of those assignments, or reducing the delay to zero (e.g. by calling DispatchQueue.main.async instead), causes the workaround to fail.
Encountered this problem in Xcode 13. Set the contentModel to center in the xib file
or
iv.contentMode = .center
I have a UIView that has a UIImageView in it.
I set the UIImageView to be circular and add a border to it like this:
self.profilePicImageView.layer.cornerRadius = self.profilePicImageView.frame.size.height / 2
self.profilePicImageView.layer.masksToBounds = true
self.profilePicImageView.layer.borderColor = UIColor.white.cgColor
self.profilePicImageView.layer.borderWidth = 3
It's being called after I initialize the view in my viewController and before I add it as a subview (function called setupUI()).
It works perfectly fine on most of the devices, but on the large screen devices (iPhone 6/6s/7 Plus and iPhone X) I get the border right but the image itself isn't circular.
Please see the examples:
Regular iPhones:
Large iPhones (iPhone 6/6s/7 Plus and iPhone X):
Any idea what the problem can be and how can I fix it?
Thanks!!
Just try this code it will help you.
imageView.layer.cornerRadius = imageHeight/2
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
Is there a way I can say for a particular UIImageView:
UIImageView *imageView = <#instantiate#>
imageView.image = #"someImageFromImageAsset";
and based on the device it's rendering on, it sets the right image. Obviously I need to set the image for a particular device but I am unsure how you do it.
iOS will automatically choose the correct sized image if you just use the name of the image. Use #2x in the image file name for retina display.
Objective-C
imageView.image = [UIImage imageNamed: #"someImageName"];
Swift
imageView.image = UIImage(named: "someImageName");