Last active
August 29, 2015 14:17
-
-
Save jacks205/702605ad650594e6fcca to your computer and use it in GitHub Desktop.
Some Path and Image drawing in Swift
This file contains 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
// | |
// CustomView.swift | |
// BluetoothExample | |
// | |
// Created by Mark Jackson on 3/18/15. | |
// Copyright (c) 2015 Mark Jackson. All rights reserved. | |
// | |
import UIKit | |
class CustomView: UIView { | |
let pi : Float = 3.14159265359 | |
var gabiImage : UIImage? | |
func DEGREES_TO_RADIANS(degrees : Float) -> Float{ | |
return pi * degrees / 180; | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
var imagePath = NSBundle.mainBundle().pathForResource("gabi", ofType: "JPG") | |
// println(imagePath) | |
var image = UIImage(contentsOfFile: imagePath!) | |
// Store the image into a property of type UIImage * | |
// for use later in the class's drawRect: method. | |
self.gabiImage = image | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
// Only override drawRect: if you perform custom drawing. | |
// An empty implementation adversely affects performance during animation. | |
override func drawRect(rect: CGRect) { | |
super.drawRect(rect) | |
// Drawing code | |
let bitmapImage : UIImage? = drawImageWithBitmapGraphics(self.gabiImage) | |
if let image = bitmapImage{ | |
UIGraphicsBeginImageContext(image.size) | |
image.drawAtPoint(CGPointZero) | |
drawOval() | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
// print(newImage) | |
let imageData : NSData = UIImagePNGRepresentation(newImage) | |
// print(imageData) | |
imageData.writeToFile("/Users/markjackson/Desktop/image.png", atomically: false) | |
}else{ | |
println("No bitmapImage") | |
} | |
} | |
func drawImageWithBitmapGraphics(image : UIImage?) -> UIImage?{ | |
var newImage : UIImage? | |
if let gabi = image{ | |
println(self.bounds.width, self.bounds.height) | |
println(gabi.size.width, gabi.size.height) | |
let width : CGFloat = 300; | |
//original height / original width x new width = new height | |
let height = (gabi.size.height / gabi.size.width) * width | |
var imageRect : CGRect = CGRectMake(0, 0, width, height) | |
println(width, height) | |
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), false, UIScreen.mainScreen().scale) | |
gabi.drawInRect(imageRect) | |
let newGabi : UIImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext(); | |
newGabi.drawInRect(CGRectMake(0, 0, newGabi.size.width, newGabi.size.height)) | |
newImage = newGabi | |
}else{ | |
println("No Gabi Image :(") | |
} | |
return newImage | |
} | |
func drawOval() -> Void{ | |
//Create oval shape to draw | |
let path : UIBezierPath = UIBezierPath(ovalInRect: CGRectMake(0, 0, 200, 100)) | |
UIColor.blackColor().setStroke() | |
UIColor.redColor().setFill() | |
// Set the render colors. | |
let ref : CGContextRef = UIGraphicsGetCurrentContext() | |
// If you have content to draw after the shape, | |
// save the current state before changing the transform. | |
//CGContextSaveGState(ref); | |
// Adjust the view's origin temporarily. The oval is | |
// now drawn relative to the new origin point. | |
CGContextTranslateCTM(ref, 50, 50) | |
// Adjust the drawing options as needed. | |
path.lineWidth = 5; | |
// Fill the path before stroking it so that the fill | |
// color does not obscure the stroked line. | |
path.fill() | |
path.stroke() | |
// Restore the graphics state before drawing any other content. | |
//CGContextRestoreGState(ref); | |
} | |
func drawPentagon() -> Void{ | |
UIColor.greenColor().setFill() | |
//Create path | |
var path : UIBezierPath = UIBezierPath() | |
//Start path | |
path.moveToPoint(CGPointMake(100, 0)) | |
//Draw line | |
path.addLineToPoint(CGPointMake(200.0, 40.0)) | |
path.addLineToPoint(CGPointMake(160, 140)) | |
path.addLineToPoint(CGPointMake(40.0, 140)) | |
path.addLineToPoint(CGPointMake(0.0, 40.0)) | |
//Close path | |
path.closePath() | |
path.fill() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment