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
Related
self.backgroundColor = UIColor(patternImage: UIImage(named: "na72.png")!)
(UILABEL)
How to fit background image of label to the frame size of a label?
I should use 'self.backgroundColor = UIColor(~~)'
Because you are creating a UIColor using a patternimage, it is designed to create a tile pattern.
To work around this, you can resize your image to match the size of the frame of your label manually, or by using a function (like this one), then use your resized image as your patternimage.
Example using the above function:
let image = imageWithImage(image: UIImage(named: "na72.png")!, scaledToSize: label.frame.size)
label.backgroundColor = UIColor(patternImage: image)
This issue was also discussed here
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 want to apply a chat bubble mask to an image view, and I would like the mask image to scale to fit in the frame of the image view. This is the code I am using to create and apply the mask:
let mask = CALayer()
let maskImage = UIImage(named: "chatGif")!
mask.contents = maskImage.CGImage
mask.frame = CGRectMake(0, 0, imageView.frame.width, imageView.frame.height)
imageView.layer.mask = mask
imageView.layer.masksToBounds = true
And this is my mask:
However this is what I get:
And when I add slicing to the mask image (i.e. create resizable cap insets) I get this:
I made a minimal project that demonstrates the issue and put it here
I do not fundamentally understand why the right and bottom sides of my mask image are getting clipped out of view. The mask's frame and bounds match the image view's, and the image used for the mask is actually smaller than the image view, so if anything I would expect it to not cover the whole thing, rather than getting partially clipped.
Can someone please explain what's happening here? How do I prevent this behavior?
EDIT: I've been playing around with this some more, and the results make a little more sense when I set the mask's size to match the maskImage size like so:
let mask = CALayer()
let maskImage = UIImage(named: "chatGif")!
mask.contents = maskImage.CGImage
mask.frame = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height)
imageView.layer.mask = mask
imageView.layer.masksToBounds = true
This gives me:
Or with slicing on the mask asset, I get the image in J.Hunter's answer. This is still not what I want because the mask is not being stretched to fill the imageView and a large portion around the sides is masked out that shouldn't be, but I can at least understand what's happening in this case. I am setting the mask's frame to a size smaller than the imageView's frame so of course it doesn't fill the entire area.
However I would expect setting the frame of the mask to match that of the imageView would make the right and bottom edges of the mask that I can see in that last screenshot line up with the right and bottom edges of the imageView. If I print out the masks's frame, bounds, and contentsRect, they are exactly what I would expect: the frame and bounds match the imageView and the contentsRect is (0.0, 0.0, 1.0, 1.0). It was my understanding that a CALayer will display a portion of its contents based on the contentsRect, so unless that rect was smaller than 1.0x1.0, the entire contents image should be displayed. Right? But why is that not what I'm seeing?
I download your demo project and rewrite some codes like this:
override func viewDidLoad() {
super.viewDidLoad()
let mask = CALayer()
let maskImage = UIImage(named: "chatGif")!
print("image size is \(maskImage.size)")
mask.contents = maskImage.CGImage
mask.frame = CGRect(origin: CGPointZero, size: maskImage.size)
imageView.layer.mask = mask
imageView.layer.masksToBounds = true
}
the image size of your maskImage is (39.5, 32.5). It looks like something wrong with your chatGif.png
the snapshot
Okay, I finally figured out what's going on. I added the same debug print statements to the sample project I made that I had put in my actual project and discovered that the imageViews frame at the time I was setting the mask was much larger that I expected it, and not its final display size. That makes sense because in the sample project I was setting the mask in viewDidLoad, before any subviews had been laid out.
I didn't realize this was the case when printing out values in my actual project because my UIImageView is in a UITableViewCell, so it's un-initialized size was the same as the placeholder size in the storyboard, which wasn't that much larger than the final size, so it didn't read as obviously too big.
Now if only I could get the last 4 hours of my life back.
EDIT: To clarify, the initial code works fine if it's run after the image view has it's final layout size set. So you should set the mask in viewDidLayoutSubviews, or if it's a table/collection cell you can check if the frame size has changed in layoutSubviews.
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.
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.. :)