2016-08-02 76 views
2

在我的iOS应用程序中,我采取了UIImageView的屏幕截图。如附件照片所示,它非常完美。但在这里,我已采取UIImageView content mode方面适合。我已经使用这个代码。截图UIImageView我只需要图像IOS

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

+0

尝试[此](http://stackoverflow.com/a/6284498/790842)来获得图像的确切大小和尺寸提供给代码当你制作截图时,尽管你也需要调整frame.x。 – iphonic

+0

获取图像视图的图像,'imageView.image' – zylenv

+0

@zylenv没有试图让那种容易的事情 – JAck

回答

0

尝试此代码:

func captureView() -> UIImage() { 

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

不错的尝试但不工作..与上面相同的结果 – JAck