Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.