Sticker cut out for iOS < 17

This commit is contained in:
Ilya Laktyushin
2024-04-26 14:23:15 +04:00
parent ff3e512869
commit b1f80d475c
13 changed files with 804 additions and 270 deletions

View File

@@ -56,6 +56,21 @@ private func generateHistogram(cgImage: CGImage) -> ([[vImagePixelCount]], Int)?
return ([histogramBinZero, histogramBinOne, histogramBinTwo, histogramBinThree], alphaBinIndex)
}
public func imageHasSubject(_ image: UIImage) -> Bool {
guard let cgImage = image.cgImage, cgImage.bitsPerComponent == 8, cgImage.bitsPerPixel == 32 else {
return false
}
if let (histogramBins, _) = generateHistogram(cgImage: cgImage) {
var totalCount: vImagePixelCount = 0
for i in 0 ..< 255 {
totalCount += histogramBins[1][i]
}
let opaqueCount: vImagePixelCount = histogramBins[1][255]
return Double(opaqueCount) / Double(totalCount) > 0.05
}
return false
}
public func imageHasTransparency(_ image: UIImage) -> Bool {
guard let cgImage = image.cgImage, cgImage.bitsPerComponent == 8, cgImage.bitsPerPixel == 32 else {
return false