Taking Screenshot of UIImageView I need only image

2019-05-28 13:28发布

问题:

In my IOS App I am taking screenshot of UIImageView. It's very perfect as shown in attachment Photo. But here, I have taken UIImageView content mode aspect fit . I've used this code.

func captureView() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false,UIScreen.mainScreen().scale)//add this line

        let context: CGContextRef = UIGraphicsGetCurrentContext()!
        self.backView.layer.renderInContext(context)
        let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }

Simply I need the snapshot of Image, Not the ImageView. Help me into this.

回答1:

Try this code:

func captureView() -> UIImage() {

    UIGraphicsBeginImageContext(YourimageView.frame.size)
    YourimageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil)
    return img
}