Get the text count from image iOS swift

Solution:

In iOS it had an option named CIDetector to detect the text count that was appeared in an image.


//    MARK: - TextCount
    func getTextCount(from img: UIImage?) -> Int {
        
        let detector = CIDetector(ofType: CIDetectorTypeText, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyLow])
        var ciImage: CIImage? = nil
        if let anImage = img?.cgImage {
            ciImage = CIImage(cgImage: anImage)
        }
        // retrive array of CITextFeature
        var features: [CIFeature]? = nil
        if let anImage = ciImage {
            features = detector?.features(in: anImage, options: [CIDetectorReturnSubFeatures: false])
        }
        return features?.count ?? 0
    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift