How to take a screenshot

Asked 1 years ago, Updated 1 years ago, 106 views

I would like to take a full screenshot of TextView, but even if I scroll through TextView, I would like to take a screenshot of the parts that I can't see, but I can't see the parts that I can't see in TextView.

func screenShot(){

    let size = CGSize (width:self.textView.contentSize.width, height:self.textView.contentSize.height)

    UIGraphicsBeginImageContextWithOptions (size, false, 0.0)
    let context —CGContext=UIGraphicsGetCurrentContext()!

    textView.layer.render(in:context)

    Let Image: UIImage=UIGraphicsGetImageFromCurrentImageContext()!

    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum (Image, nil, nil, nil)

}

I'm looking into the current situation, but

UIGraphicsBeginImageContextWithOptions (size, false, 0.0)

UIGraphicsBeginImageContextWithOptions (textView.frame.size, false, UISscreen.mainScreen().scale)

I have tried changing to , but it has not been resolved.
Please let me know.

swift ios xcode

2022-09-30 21:48

1 Answers

If you increase the size of the graphics context, the text view size remains the same, so the missing parts are not drawn.

If you want to include the invisible scroll area, expand the text view itself to fit the content and draw it in context.

If necessary, save the original size and scroll position before resizing, and restore it to its original size after taking a screenshot.

// Save after taking a screenshot (no need if you don't)
let savedFrame = textView.frame
let savedContentOffset=textView.contentOffset

// expand to fit content
textView.contentOffset=.zero
textView.frame = CGRect(origin:.zero, size:textView.contentSize)

textView.layer.render(in:context)

// Restore size and scroll position (not required)
textView.contentOffset=savedContentOffset
textView.frame = savedFrame


2022-09-30 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.