2016-08-02 89 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