Last active
August 29, 2015 14:08
-
-
Save shayne/c4659cad0bd9bd2b70f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CGViewController.swift | |
// Banksy | |
// | |
// Created by Shayne Sweeney on 11/1/14. | |
// Copyright (c) 2014 App Couture. All rights reserved. | |
// | |
import UIKit | |
class CGViewController: UIViewController { | |
var alpha: Float = 1 | |
let imageView = UIImageView() | |
let shirtMask = UIImage(named: "tshirt-mask")! | |
let shirtShadows = UIImage(named: "tshirt-shadows")! | |
let shirtHighlights = UIImage(named: "tshirt-highlights")! | |
var text = "I feel pretty, oh so pretty" | |
var shirtSize: CGSize { | |
get { | |
return self.shirtMask.size | |
} | |
} | |
var shirtRect: CGRect { | |
get { | |
return CGRectMake(0.0, 0.0, self.shirtSize.width, self.shirtSize.height) | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
imageView.frame = view.bounds | |
imageView.contentMode = UIViewContentMode.ScaleAspectFit | |
view.addSubview(imageView) | |
let textField = UITextField(frame: CGRectMake(30, 30, 290, 30)) | |
textField.text = text | |
textField.addTarget(self, action: "onTextFieldUpdate:", forControlEvents: .EditingChanged) | |
view.addSubview(textField) | |
renderShirt() | |
} | |
func onTextFieldUpdate(textField: UITextField) { | |
text = textField.text | |
renderShirt() | |
} | |
func renderShirt() { | |
UIGraphicsBeginImageContextWithOptions(shirtSize, false, 0) | |
// clip | |
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0) | |
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, -shirtRect.size.height) | |
CGContextClipToMask(UIGraphicsGetCurrentContext(), shirtRect, shirtMask.CGImage) | |
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0) | |
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, -shirtRect.size.height) | |
// fill the shirt color | |
// UIColor(white: 0.5, alpha: 1).setFill() | |
UIColor(red: 109/255, green: 108/255, blue: 108/255, alpha: 1.0).setFill() | |
// UIColor(red: 1, green: 0, blue: 0, alpha: 0.99).setFill() | |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal) | |
CGContextFillRect(UIGraphicsGetCurrentContext(), shirtRect) | |
// render text | |
renderTextImage().drawAtPoint(CGPointMake(110, 40), blendMode: kCGBlendModeNormal, alpha: 0.99) | |
// draw the highlights and shadows | |
shirtHighlights.drawAtPoint(CGPointZero, blendMode: kCGBlendModeHardLight, alpha: 0.7) | |
shirtShadows.drawAtPoint(CGPointZero, blendMode: kCGBlendModeHardLight, alpha: 0.7) | |
imageView.image = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
} | |
func renderTextImage() -> UIImage { | |
let label = UILabel() | |
label.text = text | |
label.font = UIFont(name: "F25 Executive", size: 50) | |
label.textColor = UIColor(white: 1, alpha: 1) | |
label.numberOfLines = 3 | |
let rect = CGRectMake(0, 0, 320, 320) | |
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0) | |
label.drawTextInRect(rect) | |
let textImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
UIGraphicsBeginImageContextWithOptions(shirtSize, false, 0) | |
// clip | |
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0) | |
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, -rect.size.height) | |
CGContextClipToMask(UIGraphicsGetCurrentContext(), rect, textImage.CGImage) | |
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0) | |
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, -rect.size.height) | |
// draw the highlights and shadows | |
shirtHighlights.drawAtPoint(CGPointZero, blendMode: kCGBlendModeHardLight, alpha: 0.99) | |
shirtShadows.drawAtPoint(CGPointZero, blendMode: kCGBlendModeHardLight, alpha: 0.99) | |
textImage.drawAtPoint(CGPointZero, blendMode: kCGBlendModeNormal, alpha: 0.99) | |
let output = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment