0

I am trying to do this exact example: Draw another image on a UIImage but in Swift.

I would greatly appreciate any explanation or any help in the right direction to do so.

Community
  • 1
  • 1
maz
  • 349
  • 2
  • 3
  • 15

1 Answers1

2

This is the same code from the article but in swift hope this works for you :

var width: CGFloat
var height: CGFloat
var inputImage: UIImage

    // input image to be composited over new image as example
// create a new bitmap image context at the device resolution (retina/non-retina)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), true, 0.0)
    // get context
var context: CGContextRef = UIGraphicsGetCurrentContext()
    // push context to make it current 
    // (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context)
// drawing code comes here- look at CGContext reference
// for available operations
// this example draws the inputImage into the context
inputImage.drawInRect(CGRectMake(0, 0, width, height))
// pop context 
UIGraphicsPopContext()
    // get a UIImage from the image context- enjoy!!!
var outputImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
// clean up drawing environment
UIGraphicsEndImageContext()
Nate
  • 188
  • 1
  • 11
  • I find this website useful for converting objective-c code to swift code : https://objectivec2swift.com/#/home/converter/ – Nate Jul 08 '16 at 05:15