Taking Screenshot of UIImageView I need only image IOS

落花浮王杯 提交于 2019-12-08 03:06:10

问题


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
}


来源:https://stackoverflow.com/questions/38716144/taking-screenshot-of-uiimageview-i-need-only-image-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!