Last active
July 6, 2018 23:00
-
-
Save cprovatas/66fdea17b0be653010f95f7288d100c1 to your computer and use it in GitHub Desktop.
Determine if a CGImage is completely white
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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