Skip to content

Instantly share code, notes, and snippets.

View adamcichy's full-sized avatar

Adam Cichy adamcichy

View GitHub Profile
@adamcichy
adamcichy / ImageDarkness.swift
Created March 15, 2017 15:28
Determine if a UIImage is generally dark or generally light in Swift 3
extension CGImage {
var isDark: Bool {
get {
guard let imageData = self.dataProvider?.data else { return false }
guard let ptr = CFDataGetBytePtr(imageData) else { return false }
let length = CFDataGetLength(imageData)
let threshold = Int(Double(self.width * self.height) * 0.45)
var darkPixels = 0
for i in stride(from: 0, to: length, by: 4) {
let r = ptr[i]