ios - 捕获/渲染 Scrollview 的屏幕外内容

标签 ios ipad uiscrollview render cgcontext

我也想将 ScrollView 的屏幕外内容呈现为图像。

但下面的代码只在屏幕上呈现内容,即使它应该呈现整个 scrollView 内容。

谁能提供帮助?

提前致谢!

UIImage *image = nil;

 UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, false, 0.0);
 {
        CGPoint savedContentOffset = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;

        scrollView.contentOffset = CGPointMake(0.0, 0.0);
        scrollView.frame = CGRectMake(0, 0.0, scrollView.contentSize.width, scrollView.contentSize.height);

        NSLog(@"%.2f",scrollView.contentSize.height);

        [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();

        scrollView.contentOffset = savedContentOffset;
        scrollView.frame = savedFrame;

}
UIGraphicsEndImageContext();

最佳答案

事实证明我的自动调整大小设置不正确,因此 ScrollView 的下部没有移动......

以下代码运行良好:

代码:

- (UIImage *)createPicture
{
[self setMasks:true];
UIImage *image = nil;

UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, false, 0.0);
{
    CGPoint savedContentOffset = scrollView.contentOffset;
    CGRect savedFrame = scrollView.frame;

    scrollView.contentOffset = CGPointMake(0.0, 0.0);
    scrollView.frame = CGRectMake(0, 0.0, scrollView.contentSize.width, scrollView.contentSize.height);

    NSLog(@"%.2f",scrollView.contentSize.height);

    [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();

    scrollView.contentOffset = savedContentOffset;
    scrollView.frame = savedFrame;

}
UIGraphicsEndImageContext();

[self setMasks:false];

return image;
}

- (void)setMasks:(bool)photo
{
if (photo) {
    for (label in labels)
    {
        label.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin);
    }

关于ios - 捕获/渲染 Scrollview 的屏幕外内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11557212/

相关文章:

iphone - 从 super View 中删除多个 View

iphone - 如何在多 TableView 中过滤数据

ios - 异步使用 NSXMLParser 的问题

iphone - 如何在动画与不动画时不重复此代码?

ios - 当我在 UIScrollView (Swift 3) 中滚动图像时,背景颜色渐变效果

ios - clipsToBounds 导致 UIImage 在 iOS10 & XCode 8 中不显示

ios - 是否可以指定iOS应用程序的带宽消耗

ios - 在 iOS 上,您可以在调用 applicationDidEnterBackground 之前开始在后台线程上工作吗?

ios - UIScrollView 做空白并且不滚动

iphone - UIScrollView 对象问题