Adding TextViews over UIImage - Swift - Programmatically

96 views Asked by At

I have a camera which provides me UIImage with resolution of 1920x1080, I have implemented a system where I can add UITextViews as subviews and my goal is to add them to the original UIImage and export a final Image of the resolution of 1920x1080.

So the procedure I have been thinking about is this:

  1. I add upperView over view.frame which is responsible of drawing all the textViews present on screen.

  2. Then I want to add this view over the original UIImage scaling it proportionally

This is the code I implemented in order to do it:

func passingAndDrawingTextViews(textViews : [UITextView], viewPassed : UIView, inImage : UIImage) -> UIImage{
    
    let scale = UIScreen.main.scale
    
    UIGraphicsBeginImageContextWithOptions(viewPassed.frame.size, false, scale)
    
    viewPassed.draw(CGRect(x: 0, y: 0, width: viewPassed.frame.width, height: viewPassed.frame.height))
    
    //adding each textView
    textViews.forEach { (textView) in
        let textColor = textView.textColor
        let textFont = UIFont(name: textView.font!.fontName, size: textView.font!.pointSize)
        let textAlignment = textView.textAlignment
        //alignment of the text
        let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = textAlignment
        
        let textFontAttributes = [
            NSAttributedString.Key.font: textFont,
            NSAttributedString.Key.foregroundColor: textColor,
            NSAttributedString.Key.paragraphStyle : paragraphStyle,
        ]
        
        
        let rect = CGRect(x: textView.frame.minX + textView.textContainer.lineFragmentPadding, y: textView.frame.minY + textView.textContainerInset.top, width: textView.frame.width, height: textView.frame.height)
        
        textView.text.draw(in: rect, withAttributes: textFontAttributes as [NSAttributedString.Key : Any])
    }
    

    
    
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    
    UIGraphicsEndImageContext()
    
    
    UIGraphicsBeginImageContext(inImage.size)
    
    let rect = CGRect(x: 0, y: 0, width: inImage.size.width, height: inImage.size.height)
    inImage.draw(in: CGRect(x: 0, y: 0, width: inImage.size.width, height: inImage.size.height))
    newImage!.draw(in: rect)
    let result = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return result!
    
}

In the function I respectively pass the list of textViews, the upperView, and the originalImage.

But the problem is that the textView I get are stretched on the x axis. I don't know why this happens.

What I get:

stretched

What I want

not stretched

1

There are 1 answers

2
Ali Momeni On

the rect you are passing in this line :

newImage!.draw(in: rect)

is actually the size of screen it self so the image will strech to fill this rect. so make this :

let rect = CGRect(x: 0, y: 0, width: inImage.size.width, height: inImage.size.height)

the size of the image you are passing in.