Skip to content

Instantly share code, notes, and snippets.

@cprovatas
Last active July 6, 2018 23:00
Show Gist options
  • Save cprovatas/66fdea17b0be653010f95f7288d100c1 to your computer and use it in GitHub Desktop.
Save cprovatas/66fdea17b0be653010f95f7288d100c1 to your computer and use it in GitHub Desktop.
Determine if a CGImage is completely white
import UIKit
extension CGImage {
var isBlank: Bool {
guard let data = dataProvider?.data, let buffer = CFDataGetBytePtr(data) else { return false }
let length = CFDataGetLength(data)
var i = 0
while i < length {
if buffer[i] < 255, buffer[i + 1] < 255, buffer[i + 2] < 255 {
return false
}
i += 4
}
return true
}
}
// Usage:
let img = #imageLiteral(resourceName: "MyImage")
img.cgImage?.isBlank // return true or false depending if it is all white or not
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment