Skip to main content


Showing posts from March, 2016

Review: Core Image for Swift: Advanced Image Processing for iOS by Simon J Gladman

Who is the book for? Core Image for Swift: Advanced Image Processing for iOS  is for those iOS and OS X programmers who have only really used Core Image out of necessity for when UIImage was not capable of providing a solution and now wish to go further. It is also for those who are already acquainted with Core Image and wish to delve deep into the frameworks of Core Image and Metal. At every stage and level of complexity the author, Simon Gladman, is there to guide us and never do we feel alone or set adrift in this book. First impressions The first thing I noticed about this book was its use of the Apple textbook format. This gives it a clear and concise feel. It is extremely visual, providing examples of the Core Image filters under discussion (in static and animated form) and also clear code examples that are short and readable. This sense of clarity is carried through to the text itself. There are recommendations throughout on how to optimize the performance, and it i

Swift: UIBezierPath for inscribed and circumscribed circles (Xcode 7.2.1)

UIBezierPath has an inbuilt initialiser that returns an oval in a rect and of course we can use this to draw circles by passing a square. However, assume you have a rectangular image and you want to mask it with a circle. For this you'll need the inscribed circle not an oval. The second scenario I want to imagine is a circumscribed circle outside the rect for where we want, for example, text to flow around a square image in a UITextView. In order to address this I started to think about how we draw around an image square. A square text flow is simple to achieve with a rect larger than that of the image. Circular and oval wraps pose a bigger problem: But with a little assistance from the Internet in finding the right equations all these things can be achieved: extension CGFloat { func radians() -> CGFloat { let b = CGFloat(M_PI) * (self/180) return b } } extension UIBezierPath { convenience init(circumscribedCircleRect rect:CGRect) {

Swift: Sorting togetherness with Zip2Sequence and Stable Sort (Xcode 7.2; updated Swift 3, Xcode 8)

Let's face it, of all the types in Swift, Zip2Sequence receives very little press. This doesn't mean its role in the evolution of Swift has been forgotten at Apple. It just means that generally its role is  fulfilled using other approaches ( or maybe being at the end of the alphabet us bloggers just haven't addressed its purpose yet). The basics As the name suggests, a Zip2Sequence is created by zipping together two sequences. let firstArray = [1,2,3,4,5,6,7,8,9,10] let secondArray = ["A","B","C","D","E","F","G","H","I","J"] // first way of instantiating let zipped1 = Zip2Sequence(firstArray,secondArray) // second way of instantiating let zipped2 = zip(firstArray, secondArray) Note: The two sequences preserve their original types, the number of zipped together pairs is as long as the shortest of the zipped sequences, and if one sequence is longer than the other then

Swift: Array and ArraySlice (Xcode 7.3, Swift 2.2; updated Xcode 8 beta 6, Swift 3)

** Jump to Swift 3 code ** Before we begin A great debt in writing this post is owed to a presentation by AirspeedVelocity at a Swift London event held in the Facebook offices in 2015. There he went into the depths of how arrays and other types are stored and copied in memory. Here I'm looking at a far more basic level aimed at the practical implications of Array and ArraySlice when coding. Array Let's consider a humble array of type Array<Int>: var array = [1,2,3,4,5,6,7,8,9,10] If we want to retrieve the first item in the array we can do the following: let firstItem = array.first Or we can subscript the array like so: let firstItem = array[0] In both instances we retrieve an Int value. Slicing Now let's suppose we take a range from the original array to create a new one: let slice = array[1...7] // [2, 3, 4, 5, 6, 7, 8] for i in slice { i } slice.first // 2 As you'll see we can do pretty much all the things that are available to us with

Swift: Giving context to CGContext (Part I)

First encounters with CGContext can be bewildering. We are presented with a lot of code that looks most unlike Swift and more than likely it's in a drawRect: method of a UIView. So let's consider the entry point to drawing: override func drawRect(rect: CGRect) { if let context = UIGraphicsGetCurrentContext() { } } To explain, during the lifetime of a drawRect: there is a CGContext that we can gain access to by simply calling UIGraphicsGetCurrentContext() as demonstrated above. Once we have the context, which is the place that we're going to be drawing to, we can draw. And this drawing will appear every time a view of our UIView subclass type is presented on screen. Demonstrating CGContext To demonstrate this, the first thing we're going to do is to fill the view and to draw a line around it. But the first thing any writer of Swift code will notice is that placing a dot after the context instance name results in no options popping up whatsoever. So what