In iOS application, I have 250x85pt UIView. I would like it to have image-based tiled background. The original image is 398x398 px. I would like my image to tile.
When I run application, my image tiles, but for some reason it scales as follows:
I used the following code to achieve it:
var image = UIImage(contentsOfFile: "myfilepath.png")!
var uicolor = UIColor(patternImage: image)
uiview.backgroundColor = uicolor
Could you explain 1. why ios scales my image, and 2. how do I draw tiled image without scaling?
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .
Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.
An object that manages image data in your app.
A view that displays a single image or a sequence of animated images in your interface.
basically the image is bigger than your UIView
. when you give UIColor(patternImage: image)
it uses 1x size of the image.
The image we usually laptops are scaled to window size with its aspect ratio.
So scale your image to your requirement and then apply it as background image.
To expand on Banto's answer, the issue is that your view is 250x85 points, which, on retina devices is really 500x170 pixels. When your x1 image is turned into a pattern it is applied at the point level. The problem can be easily changed by setting the scale on the image to match the scale on the device:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 85))
let image = UIImage(named: "myfilepath.png")!
let scaled = UIImage(CGImage: image.CGImage, scale: UIScreen.mainScreen().scale, orientation: image.imageOrientation)
view.backgroundColor = UIColor(patternImage: scaled!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With