截取UIImageView的截图我只需要图像IOS

时间:2016-08-02 09:04:15

标签: ios swift uiimageview uiimage screenshot

在我的IOS App中,我正在截取UIImageView的截图。它非常完美,如附件照片中所示。但在这里,我采用了UIImageView content mode 方面适合。我用过这段代码。

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
    }

我只需要Image的快照,而不是ImageView。帮帮我。enter image description here

1 个答案:

答案 0 :(得分:0)

试试这段代码:

func captureView() -> UIImage() {

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