2016-04-06 25 views
0

我想截取UIImageView和UILabel的截圖。UILabel的屏幕截圖和UIImageView中的內容

我到目前爲止已經抓住了ImageView中的UIImage,然後渲染了疊加層,但是UILabel的定位都是錯誤的。我將捕捉的大小設置爲實際圖像大小(這不是我想要的)。

我只是想能夠截圖如何在屏幕上顯示它。

CGSize imageSize = self.imageFromOtherView.size; 
// define the size and grab a UIImage from it 
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 

[self.imageFromOtherView drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)]; 

[self.socialLabel.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

您是否希望獲得整個窗口或屏幕上圖像的截圖?你的問題有點難以分辨。 – fsb

回答

1

創建一個UIView來保存UIImageView和UILabel。然後通過這個代碼傳遞這個容器視圖。我把我的視圖看作一個名爲viewForPhoto的屬性,所以當代碼被調用時它只捕獲該視圖。你可以調整它,以便它接收到一個視圖。這將返回你想要的UIImage。

- (UIImage *)imageByRenderingView 
{ 
    UIGraphicsBeginImageContextWithOptions(self.viewForPhotoView.bounds.size, NO, 0.0); 
    [self.viewForPhotoView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return resultingImage; 
} 
+0

感謝您的幫助! – Ro4ch

1

將這兩個視圖添加到通用容器視圖,然後在視圖上調用- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates以在上下文中呈現它。