Core Image and Graphic context in swift

Graphic context

// size: 200x200, opaque: true, scale: 1(0:screen scale)
UIGraphicsBeginImageContextWithOptions(CGSize(width: 200, height: 200), true, 1)

// drawing commands
CGContextSetRGBFillColor (context, 1, 1, 0, 1);
// ...

// getting an image from it
let image = UIGraphicsGetImageFromCurrentImageContext()

Core image with filter

// create a UIImage
let image = UIImage(named: "c0415")
// create Core Image context
let ciContext = CIContext(options: nil)
// create a CIImage(image data for processing)
let coreImage = CIImage(image: image)
// picking the filter
//let filter = CIFilter(name: "CIPhotoEffectTransfer")
let filter = CIFilter(name: "CIVignetteEffect")
// passing image
filter.setValue(coreImage, forKey: kCIInputImageKey)
// set a custom value for the inputCenter
filter.setValue(CIVector(x: xpos, y: ypos), forKey: kCIInputCenterKey)
// retrieve the processed image
let filteredImageData = filter.valueForKey(kCIOutputImageKey) as CIImage
// returns a Quartz image from the Core Image context
let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent())
// this is our final UIImage ready to be displayed
let filteredImage = UIImage(CGImage: filteredImageRef);

drawing text

// select a font
let font = UIFont.boldSystemFontOfSize(44)
let showText:NSString = "hello world"
// setting attr: font name, color...etc.
let attr = [NSFontAttributeName: font, NSForegroundColorAttributeName:UIColor.whiteColor()]
// getting size
let sizeOfText = showText.sizeWithAttributes(attr)

let image = UIImage(named: "c0415")
let rect = CGRectMake(0, 0, image!.size.width, image!.size.height)

UIGraphicsBeginImageContextWithOptions(CGSize(width: rect.size.width, height: rect.size.height), true, 0)

// drawing our image to the graphics context
// drawing text
showText.drawInRect(CGRectMake(rect.size.width-sizeOfText.width-10, rect.size.height-sizeOfText.height-10, rect.size.width, rect.size.height), withAttributes: attr)

// getting an image from it
let newImage = UIGraphicsGetImageFromCurrentImageContext();