7

The code below prints a line on a UIView. I just want to know the code that I would write to be able to insert an image on top of the view.

import UIKit

class draw: UIView {
    var line = UIBezierPath()
    var line1 = UIBezierPath()

    func grapher() {
        line1.move(to: .init(x:0, y: bounds.height / 6))
        line1.addLine(to: .init(x: bounds.width, y: bounds.height / 6))
        UIColor.blue.setStroke()
        line1.lineWidth = 2
        line1.stroke()
    }

    override func draw(_ rect: CGRect) {
        grapher()
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • you can simple add background image in view why you can draw bezierpath? – Sagar Bhut Jan 10 '18 at 05:09
  • You looking for [`UIImageView`](https://developer.apple.com/documentation/uikit/uiimageview)? What does the line drawing have to do with inserting an image? – shim Jan 10 '18 at 05:21
  • please check [https://stackoverflow.com/questions/6905941/objective-c-draw-line-on-top-of-uiimage-or-uiimageview](https://stackoverflow.com/questions/6905941/objective-c-draw-line-on-top-of-uiimage-or-uiimageview) – Noot Jan 10 '18 at 05:38

3 Answers3

14

Add UIImage on the view layer

let myLayer = CALayer()
let myImage = UIImage(named: "star")?.cgImage
myLayer.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
myLayer.contents = myImage
myView.layer.addSublayer(myLayer)

Add UIImage as a subView

let image = UIImage(named: "star")
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
myView.addSubview(imageView)
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
10

If you want to add image on view. Please use below code.

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
self.view.addSubview(imageView)
//Imageview on Top of View
self.view.bringSubview(toFront: imageView)

If you want to draw specific shape then go with Core Graphics

https://developer.apple.com/documentation/coregraphics

https://cocoacasts.com/drawing-shapes-in-swift-with-paintcode/

https://www.ioscreator.com/tutorials/drawing-shapes-core-graphics-tutorial-ios10

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
0

Tested on Swift 4

This function should be called in/after viewDidAppear to let view render

func addArrowImageToButton(button: UIButton, arrowImage:UIImage = #imageLiteral(resourceName: "my_arrow_image") ) {
    let btnSize:CGFloat = 32
    let imageView = UIImageView(image: arrowImage)
    let btnFrame = button.frame
    imageView.frame = CGRect(x: btnFrame.width-btnSize-8, y: btnFrame.height/2 - btnSize/2, width: btnSize, height: btnSize)
    button.addSubview(imageView)
    //Imageview on Top of View
    button.bringSubviewToFront(imageView)
}

and call it this way in viewDidAppear

self.addArrowImageToButton(button: myButtonObject)
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
  • Can you tell me exactly what code to write in viddidappear. –  May 10 '19 at 21:54
  • @SamBurns I have updated my answer for the same. `myButtonObject` needs to be instance of button that needs to have arrow image. `my_arrow_image` needs to be replaced with the name of your arrow image. – jeet.chanchawat May 15 '19 at 08:25