Ios 屏幕快照不提供整个屏幕的图像
我正在制作与图像相关的应用程序。我的屏幕上有多个图像。我已经拍了那张照片。但它不应该提供我的整个屏幕。 最顶部和最底部的一小部分不需要显示在该图中 我在顶部有导航栏。底部还有一些按钮。我不想在截图图像中捕捉按钮和导航栏。 下面是我的屏幕截图代码Ios 屏幕快照不提供整个屏幕的图像,ios,iphone,uiimageview,screenshot,Ios,Iphone,Uiimageview,Screenshot,我正在制作与图像相关的应用程序。我的屏幕上有多个图像。我已经拍了那张照片。但它不应该提供我的整个屏幕。 最顶部和最底部的一小部分不需要显示在该图中 我在顶部有导航栏。底部还有一些按钮。我不想在截图图像中捕捉按钮和导航栏。 下面是我的屏幕截图代码 -(UIImage *) screenshot { UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [UIScreen mainScreen].scale);
-(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];
您可以使用下面的代码拍摄视图的屏幕截图 我已经设置了检查屏幕截图大小的条件 使用此代码,图像将保存在您的文档文件夹中,从那里您可以使用图像在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];
希望它能对您有所帮助。实现这一点的最简单方法是添加一个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;
}
希望这有帮助 使用此代码
-(IBAction)captureScreen:(id)sender
{
UIGraphicsBeginImageContext(webview.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}
样本项目
屏幕更新后的快照视图 但它仅在iOS 7.0及更高版本中可用
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates
此方法从渲染服务器捕获屏幕的当前可视内容,并使用它们构建新的快照视图。您可以使用返回的快照视图作为应用程序中屏幕内容的可视替代。例如,您可以使用快照视图来促进全屏动画。由于内容是从已渲染的内容中捕获的,因此此方法反映屏幕的当前视觉外观,并且不会更新以反映已计划或正在进行的动画。但是,此方法比尝试自己将屏幕内容渲染为位图图像要快
我尝试使用uiimageview代替UIView,但它对我没有帮助。让我们试试你的代码。