Taking Screenshot of UIImageView I need only image

2019-05-28 12:54发布

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.enter image description here

1条回答
【Aperson】
2楼-- · 2019-05-28 13:34

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
}
查看更多
登录 后发表回答