2016-08-02 18 views
0

enter image description hereenter image description here在我的iOS應用程序的代碼,我服用的UIImageView的截圖,但我需要它的時候它不是通過下面的代碼正確服用。截圖是不是在適當的大小IOS

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中執行圖像的屏幕截圖,因爲它上面有更多的圖像。 請建議我適當的代碼獲得確切的UIImageView截圖

+0

添加UIScreen.mainScreen().scale,而不是0.0我想的ImageView的圖像exect屏幕截圖,因爲有它更多的圖像。手段顯示我的屏幕截圖 –

+0

這是簡單的imageView ..我想拍的那個.. – JAck

+0

只是Imageview不是總屏幕 –

回答

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! 
     } 
+0

是什麼畫ViewHierchyinRect() – JAck

+0

請檢查[這](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/ drawViewHierarchyInRect:afterScreenUpdates :) – Rurouni

+0

我問你的方法.. drawViewHierarchyInRect(邊界,afterScreenUpdat ES:真) – JAck