2016-04-22 16 views
2

我在swift中使用下面的代碼將UIImageView捕獲到一個圖像中。它可以工作,但圖像質量不如UIImageView上顯示的質量。捕捉此圖像時是否有配置質量的方法?在swift中渲染UIImageView時質量鬆散

private func getScreenshow(imageView:UIImageView) -> UIImage{ 

    UIGraphicsBeginImageContext(self.imageView.frame.size) 
    let context = UIGraphicsGetCurrentContext() 

    imageView.layer.renderInContext(context!) 
    let screenShot = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    UIImageWriteToSavedPhotosAlbum(screenShot, nil, nil, nil) 
    return screenShot 
} 

回答

2

經過一番搜索,我發現了這個問題。我使用下面的代碼來替換「UIGraphicsBeginImageContext」,它的工作原理。

UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, true, 0) 
2

該代碼看起來很奇怪(你爲什麼不只是使用imageView.image?),但我不知道你的用例的完整的上下文。

正如您發現的,質量損失的原因是您忽略了屏幕的視網膜比例。

閱讀UIGraphicsBeginImageContextUIGraphicsBeginImageContextWithOptions的文件,你會看到前者使用「的1.0規模因子」 a。

+0

因爲在imageView上顯示了許多其他子視圖。 –

+0

按問題與此問題無關,因爲我正在將UIBezierPath轉換爲UIImage。然而,你對比例因子的評論是我忽略的。出於某種原因,我把scale設爲1.我做到了0,一切看起來很美!謝謝! :) –