Reputation: 1070
I have a UIImage
that I'm trying to display at a particular point, but when I call:
image.draw(at: CGPoint.zero)
the image never shows. However, if I create a UIImageView
with the image and add that imageview to one of the UIStackViews
in my view hierarchy, the image is clearly shown. How can I draw my image at the window's origin?
Upvotes: 0
Views: 59
Reputation:
Those draw methods don't draw to UIWindow
s / UIView
s, they draw to the current CGContext
.
Check out the documentation: https://developer.apple.com/reference/uikit/uiimage/1624132-draw "Draws the image at the specified point in the current context."
For example:
UIGraphicsBeginImageContext(image.size)
image.draw(at: CGPoint.zero)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
If you want to have some sort of image view or whatever to "float above" your application, you can do that with a container view controller.
Here's a playground that shows a quick example where the red square is an image view "floating" above the application:
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
class ContainerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
addImageViewOverlay()
}
func addImageViewOverlay() {
let overlayImageView = UIImageView()
overlayImageView.backgroundColor = UIColor.red
overlayImageView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(overlayImageView)
overlayImageView.widthAnchor.constraint(equalToConstant: 50.0).isActive = true
overlayImageView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
overlayImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10.0).isActive = true
overlayImageView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10.0).isActive = true
}
func embed(childController: UIViewController) {
// In a real application you'd remove the existing child controller first
addChildViewController(childController)
childController.view.translatesAutoresizingMaskIntoConstraints = false
view.insertSubview(childController.view, at: 0)
childController.didMove(toParentViewController: self)
childController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
childController.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
childController.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
childController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
let containerController = ContainerViewController(nibName: nil, bundle: nil)
containerController.view.frame = CGRect(x: 0.0, y: 0.0, width: 320.0, height: 480.0)
let applicationController = UITabBarController(nibName: nil, bundle: nil)
let tabOneController = UIViewController(nibName: nil, bundle: nil)
tabOneController.title = "One"
let tabTwoController = UIViewController(nibName: nil, bundle: nil)
tabTwoController.title = "Two"
applicationController.viewControllers = [tabOneController, tabTwoController]
containerController.embed(childController: applicationController)
PlaygroundPage.current.liveView = containerController.view
Upvotes: 4