Skip to content

Instantly share code, notes, and snippets.

@SpacyRicochet
Last active August 29, 2015 14:22
Show Gist options
  • Save SpacyRicochet/0eb057d8cf1bca540655 to your computer and use it in GitHub Desktop.
Save SpacyRicochet/0eb057d8cf1bca540655 to your computer and use it in GitHub Desktop.
drawViewHierarchyFromRect fails to work.
//: Playground - noun: a place where people can play
import UIKit
let view = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
if let calendarIcon = UIImage(named: "Events") {
let calendarView = UIImageView(image: calendarIcon)
view.addSubview(calendarView)
}
let date = NSDate(timeIntervalSinceNow: 4*30*24*60*60)
let dateFormatter = NSDateFormatter()
// Create the month
dateFormatter.dateFormat = "MMM"
let month = dateFormatter.stringFromDate(date).uppercaseString
let monthLabel = UILabel(frame: CGRect(x: 8, y: 4, width: 14, height: 4))
monthLabel.text = month
monthLabel.font = UIFont.boldSystemFontOfSize(6)
monthLabel.textAlignment = .Center
monthLabel.textColor = UIColor.whiteColor()
view.addSubview(monthLabel)
// Create the day number
dateFormatter.dateFormat = "dd"
let day = dateFormatter.stringFromDate(date)
let numberLabel = UILabel(frame: CGRect(x: 4, y: 9, width: 22, height: 17))
numberLabel.text = day
numberLabel.font = UIFont.boldSystemFontOfSize(14)
numberLabel.textAlignment = .Center
view.addSubview(numberLabel)
println("Everything seems to be working fine.")
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 2)
//view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
view.layer.renderInContext(UIGraphicsGetCurrentContext()) // Interestingly enough, this does work :/
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let imageView = UIImageView(image: image)
println("However, here nothing updates anymore.")
extension UIImage
{
class func calendarTabImageWithCurrentDate() -> UIImage
{
return calendarTabImageWithDate(NSDate())
}
class func calendarTabImageWithDate(date: NSDate) -> UIImage
{
let dateFormatter = NSDateFormatter()
let view = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
let calendarView = UIImageView()
calendarView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
if let calendarImage = UIImage(named: "EventsTabIcon") {
calendarView.image = calendarImage
}
view.addSubview(calendarView)
// Create the month
dateFormatter.dateFormat = "MMM"
let month = dateFormatter.stringFromDate(date).uppercaseString
let monthLabel = UILabel(frame: CGRect(x: 8, y: 4, width: 14, height: 4))
monthLabel.text = month
monthLabel.font = UIFont.boldSystemFontOfSize(6)
monthLabel.textAlignment = .Center
monthLabel.textColor = UIColor.whiteColor()
view.addSubview(monthLabel)
// Create the day number
dateFormatter.dateFormat = "dd"
let day = dateFormatter.stringFromDate(date)
let numberLabel = UILabel(frame: CGRect(x: 4, y: 9, width: 22, height: 17))
numberLabel.text = day
numberLabel.font = UIFont.boldSystemFontOfSize(14)
numberLabel.textAlignment = .Center
view.addSubview(numberLabel)
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.mainScreen().scale)
//view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment