UIImage Orientation Issue - ios

Why does the background image render as it does on image 2? The portrait mode looks fine, but when I change the phone to landscape mode, it seems that the background image renders the same image multiple times to fit the entire width of the screen. Why does this happen and how can I resolve this?
Edit 1:
This is the code that I use to demonstrate this issue:
let backgroundImage = UIImage(named: "Background")
view.backgroundColor = UIColor(patternImage: backgroundImage!)
Image 1:
Image 2:

I created a UIImageView as Alladinian suggested, which helped but it didn't scale properly with the x.contentMode property (I haven't looked into why yet). You have to use the autoresizingMask property on your UIImageView declaration in order to make the background image fit properly in all content modes.
This will make it work:
let backgroundImage = UIImage(named: "Background")
let backgroundImageView = UIImageView(image: backgroundImage)
backgroundImageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
view.addSubview(backgroundImageView)

Related

How to set uiview background and uiimageview to be centered, scaled to fill and masked to bounds?

Hello I am working on feed and I am facing issues with displaying images correctly. The issue is with first two UITableViewCells, that get displayed after tableView loads. The remaining cells load correctly. The code is mention below that I am using to display the images.
Following are some screenshots of the issue.
https://ibb.co/W3rCsmB
https://ibb.co/8PZKVSh
https://ibb.co/jhwm6JH
https://ibb.co/cLyfC3C
//Code to add Image.
//Code written in configurecell()
let imageURL = URL(string: ForFeed.image_url)
let media = UIImageView(frame: mediaView.bounds)
media.downloaded(from: imageURL!)
media.contentMode = UIViewContentMode.scaleAspectFill
media.layer.masksToBounds = true
self.mediaView.insertSubview(media, at: 0)
//Code to add Background image.
//Code written in awakefromnib()
UIGraphicsBeginImageContext(self.mediaView.frame.size)
let mediaPlaceholder = UIImage(named: "nilmedia")
mediaPlaceholder?.draw(in: self.mediaView.bounds)
if let image = UIGraphicsGetImageFromCurrentImageContext(){
UIGraphicsEndImageContext()
self.mediaView.backgroundColor = UIColor(patternImage: image)
}else{
UIGraphicsEndImageContext()
debugPrint("Image not available")
}
The result should display images properly.
Just headover to storyboard and select he image view and then go to Attribute inspector
set contentView to aspect fill

Correct placement of UIImageView in UIBarButtonItem

When attempting to place a UIImageView(UIImage) into a UIBarButtonItem on a UINavigationBar, the image gets placed in the middle of the bar and also has wide fields covering the entire bar. So, doesn't look like a small button on the left.
I've tried various tricks with frame resizing, contentMode settings.
The below code is from my View Controller, which is part of the Navigation Controller stack. Added this image into Assets:
http://pluspng.com/img-png/png-hd-bike-ktm-bike-png-500.png
for testing, named it bike.png and used it in UIImage below.
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "bike")
let imageView = UIImageView(image: image)
imageView.backgroundColor = .blue //for debugging
imageView.contentMode = .scaleAspectFit
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: imageView)
}
The expected result would be to have the image of the motorbike in the left, rather than in the middle. Also, no empty fields to the left and to the right (highlighted in blue for debugging) of the image.
EXPECTED (drew up in Paintbrush):
REALITY:
Ended up resolving as:
imageView.widthAnchor.constraint(equalToConstant: (navigationController?.navigationBar.frame.height)!).isActive = true

UITextView add background image in ios

I am trying for add image as background of UITextview
textView.backgroundColor = UIColor(patternImage: UIImage(named: "Background")!)
Output is :-
Textview not display single image ?
You need to set image in layer of textView instead of setting backgroundColor with UIColor(patternImage:). So set image this way.
textView.layer.contents = UIImage(named: "Background")!.cgImage

Fixed background in a TableView?

Fixed Background image in a TableView ?
Hey guys !
My first question as a Swift nOOb !
I'm trying to set up a fixed image as a background for my Table View. So far, the best option has been to include this in my ViewDidLoad :
let uluru = UIImage(named: "Uluru")
self.view.backgroundColor = UIColor(patternImage: uluru!)
Not so great, right?
Especially because when you're scrolling, the image is tiled. Meaning, it's repeating itself. Does anyone has a solution via the IB or directly into the code to make it fixed ? Something like in CSS ?
I also tried the superview way :
override func viewDidAppear(animated: Bool) {
let uluru = UIImage(named: "Uluru")
let uluruView = UIImageView(image: uluru)
self.view.superview!.insertSubview(uluruView, belowSubview:self.view)
}
But no success at all!
And last but not least :
override func viewDidLoad() {
super.viewDidLoad()
let backgroundImage = UIImageView(frame: UIScreen.mainScreen().bounds)
backgroundImage.image = UIImage(named: "Uluru")
self.view.insertSubview(backgroundImage, atIndex: 0)
}
Thank you all!
Do not use the backgroundColor property for this, and do not add any subviews. The table view is all ready for you to do what you want to do. Like this:
Create an image view (UIImageView) whose image is the desired image.
Make that image view the table view's background view (its backgroundView property).

Navigation Controller image instead of text

I am trying to put a logo (png) file into my navigation controller. I found one solution that "worked" but it didn't scale down the image to fit, thus is was massive
This is the code im using thus far, but it makes the image huge
let logoImage:UIImage = UIImage(named: "logo.png")!
self.navigationItem.titleView = UIImageView(image: logoImage)
Try this:
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
imageView.contentMode = UIViewContentMode.ScaleAspectFit //you need to set this.
self.navigationItem.titleView = imageView
And if you want to give height and width then add this:
imageView.frame.size.width = 200
imageView.frame.size.height = 45
You can do it in 2 ways: programmatically or via XCode:
An easy way is via XCode. For it, drag view from the object library and drag it to your navigation bar. Later drag into your view in navigation bar UIImageView from object library. That's all.
Or you can do all of this programmatically.
var myView = UIView()
var imageView = UIImageView()
imageView.image = UIImage(named: "your_image_here")
myView.addSubview(imageView)
navigationController?.navigationBar.addSubview(myView)

Resources