Skip to content

Instantly share code, notes, and snippets.

@fabnoe
Created May 14, 2015 21:09
Show Gist options
  • Save fabnoe/de54136005db6c006b55 to your computer and use it in GitHub Desktop.
Save fabnoe/de54136005db6c006b55 to your computer and use it in GitHub Desktop.
class StartViewController : UIViewController {
private var contextQueue_:dispatch_queue_t? = nil
private var originalImage:UIImage?
@IBOutlet weak var imageView: UIImageView!
@IBAction func convert(sender: AnyObject) {
applyFilter()
}
override func viewDidLoad() {
super.viewDidLoad()
originalImage = imageView.image
contextQueue_ = dispatch_queue_create("ly.img.filterPreviewQueue", DISPATCH_QUEUE_SERIAL)
}
func applyFilter() {
var image = self.originalImage!
dispatch_async(contextQueue_!, {
autoreleasepool {
var type = IMGLYFilterType(rawValue: Int.random(1...10))!
//type = IMGLYFilterType.Mono3200
var actualFilter:CIFilter? = IMGLYInstanceFactory.sharedInstance.effectFilterWithType(type)
var filteredImage:UIImage? = IMGLYPhotoProcessor.processWithUIImage(image, filters: [actualFilter!])
var text = actualFilter!.displayName
actualFilter = nil
dispatch_async(dispatch_get_main_queue(), {
self.imageView.image = filteredImage!
filteredImage = nil
})
}
})
}
}
extension Int
{
static func random(range: Range<Int> ) -> Int
{
var offset = 0
if range.startIndex < 0 // allow negative ranges
{
offset = abs(range.startIndex)
}
let mini = UInt32(range.startIndex + offset)
let maxi = UInt32(range.endIndex + offset)
return Int(mini + arc4random_uniform(maxi - mini)) - offset
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment