Skip to content

Instantly share code, notes, and snippets.

@niftycode
Last active January 27, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftycode/f8f460d2d4c4a0879d7e to your computer and use it in GitHub Desktop.
Save niftycode/f8f460d2d4c4a0879d7e to your computer and use it in GitHub Desktop.
This playground project shows how to modify an image with CALayer (Swift 4)
//: # CALayer Example 2
import UIKit
import PlaygroundSupport
// view with white background color
var backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
backgroundView.backgroundColor = UIColor.white
PlaygroundPage.current.liveView = backgroundView
var imageView = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 250))
imageView.center = backgroundView.center
let myImage = UIImage(named: "nature.jpg")
backgroundView.addSubview(imageView)
imageView.layer.contents = myImage?.cgImage
imageView.layer.borderWidth = 4
imageView.layer.borderColor = UIColor.green.cgColor
imageView.layer.cornerRadius = imageView.bounds.height/2.0
imageView.layer.masksToBounds = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment