屏幕截图的大小不合适IOS

时间:2016-08-02 06:06:13

标签: ios swift image screenshot

enter image description here enter image description here在我的IOS应用程序代码中,我正在截取UIimageView的截图,但是当我接受它时,通过以下代码无法正常使用它。

func captureView() -> UIImage {
//        let rect: CGRect = self.imageView.bounds

        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, 0.0)//add this line

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

我想要在imageView中对图像进行exect屏幕截图,因为它上面有更多图像。 请建议我获取准确的UIImageView截图

的正确代码

1 个答案:

答案 0 :(得分:0)

如何在UIScreen.mainScreen().scale

中添加0.0而不是UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
func captureView() -> UIImage {
            UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
            drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
            let img = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return img!
        }