Search code examples
iosswiftcore-graphicsdraw

Position an image with Swift and CoreGraphics


I'm working on a drawing app using Swift and it's my first time using CoreGraphics. My app is a drawing with two steps:

  1. Drawing then pass the image to a UICollectionView
  2. Take the image from the UICollectionView and draw above it.

What I need to know is how to position the image at the bottom of the hierarchy. Here's an example of my code, which positions the image in the center:

import UIKit

class TheViewController: UIViewController {

  @IBOutlet weak var drawingPlace: UIImageView!
  var theFisrtImageFromStep1 = arrayOfPlans[choseOne]

  override func viewDidLoad() {
    drawingPlace.image = theFisrtImageFromStep1
  }

}

Solution

  • solve my problem :

    func resize(collectionImage : UIImage) -> UIImage{
            let point = CGPoint(x: drawingPlace.frame.width / 4, y: drawingPlace.frame.height - 300)
            UIGraphicsBeginImageContext(drawingPlace.frame.size)
            collectionImage.draw(in: CGRect(origin:  point , size: CGSize(width: drawingPlace.frame.size.width / 2, height: drawingPlace.frame.size.height / 2)))
    
            let img = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return img!
        }
    

    in viewDidLoad :

    drawingPlace.image = resize(collectionImage: arrayOfPlans[choseOne])
    

    this function : resize my image , position the image in the bottom and thats what i need , thank you .