2016-04-06 26 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以在上下文中呈现它。