屏幕截图不提供整个屏幕的图像

我正在制作与图像相关的应用程序。 我的屏幕上有多个图像。 我拍了那个屏幕截图。 但它不应该提供我的整个屏幕。 最顶层和最底层部分的一小部分无需显示。

我的导航栏在上面。 底部还有一些按钮。 我不想在我的截图图像中捕获那些按钮和导航栏。 下面是我的屏幕截图代码。

-(UIImage *) screenshot { UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [UIScreen mainScreen].scale); [self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

截取屏幕截图后,我通过facebook分享方法中的以下代码使用它,

 UIImage *image12 =[self screenshot]; [mySLComposerSheet addImage:image12]; 

实现这一目标的最简单方法是添加一个UIView,它包含你想截取屏幕截图的所有内容,然后从UIView而不是主UIView调用drawViewHierarchyInRect

像这样的东西:

 -(UIImage *) screenshot { UIGraphicsBeginImageContextWithOptions(contentView.bounds.size, YES, [UIScreen mainScreen].scale); [contentView drawViewHierarchyInRect:contentView.frame afterScreenUpdates:YES]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

希望这可以帮助!

您可以使用我的下面的代码来截取视图的屏幕截图。

我已经把条件检查屏幕截图的大小。

使用此代码图像保存在您的文档文件夹中,您可以使用该图像在Facebook或任何您想要共享的位置共享。

 CGSize size = self.view.bounds.size; CGRect cropRect; if ([self isPad]) { cropRect = CGRectMake(110 , 70 , 300 , 300); } else { if (IS_IPHONE_5) { cropRect = CGRectMake(55 , 25 , 173 , 152); } else { cropRect = CGRectMake(30 , 25 , 164 , 141); } } /* Get the entire on screen map as Image */ UIGraphicsBeginImageContext(size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); /* Crop the desired region */ CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect); UIImage * cropImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); /* Save the cropped image UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil);*/ //save to document folder NSData * imageData = UIImageJPEGRepresentation(cropImage, 1.0); NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; NSString *imagename=[NSString stringWithFormat:@"Pic.jpg"]; NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imagename]; ////NSLog(@"full path %@",fullPathToFile); [imageData writeToFile:fullPathToFile atomically:NO]; 

希望它能帮到你。

使用此代码

  -(IBAction)captureScreen:(id)sender { UIGraphicsBeginImageContext(webview.frame.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); } 

示例项目www.cocoalibrary.blogspot.com

http://www.bobmccune.com/2011/09/08/screen-capture-in-ios-apps/

snapshotViewAfterScreenUpdates

但它仅适用于iOS 7.0及更高版本

 - (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates 

此方法从呈现服务器捕获屏幕的当前可视内容,并使用它们构建新的快照视图。 您可以使用返回的快照视图作为应用程序中屏幕内容的可视替代。 例如,您可以使用快照视图来促进全屏动画。 由于内容是从已渲染的内容中捕获的,因此该方法反映了屏幕的当前视觉外观,并且未更新以反映已安排或正在进行的动画。 但是,此方法比尝试自己将屏幕内容呈现为位图图像更快。

https://developer.apple.com/library/ios/documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html#//apple_ref/occ/instm/UIScreen/snapshotViewAfterScreenUpdates: