iOS 7中drawViewHierarchyInRect:afterScreenUpdates:在iPad上出现黑屏问题

4

我遇到了一个非常奇怪的情况...我试图通过调用下面的drawViewHierarchyInRect: afterScreenUpdates:方法来捕获滚动视图中(其contentSize超出屏幕边界三倍)的内容:

+ (UIImage *) imageFromScrollView: (UIScrollView *) scrollView {

    UIGraphicsBeginImageContext(size);

    [view drawViewHierarchyInRect: CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)
               afterScreenUpdates: YES];

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();  

    return image;  
}

该应用适用于:iPhone设备、iPhone和iPad模拟器,但在iPad设备上不可用。您是否遇到过类似的问题?我会非常感激任何帮助!


你尝试过使用[view.layer renderInContext:UIGraphicsGetCurrentContext()];来查看是否有不同的结果吗?此外,它不应该使图像完全变黑,但另一件事是你不应该真的使用UIGraphicsBeginImageContext()。而是使用UIGraphicsBeginImageContextWithOptions()。这样它将正确显示在视网膜/非视网膜设备上。 - daveMac
我也尝试过使用UIGraphicsGetCurrentContext()UIGraphicsBeginImageContext()。之前,我使用了你提到的renderInContext:方法,但我想利用iOS 7中的新方法,这似乎更快。 - jaaakub
2个回答

3

使用

UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

在这里,NO很重要,因为它返回一个非不透明的图像。


-1

这段代码对我来说可行。

UIImage *image = [SnapShotter snapShotEntireScrollingView:self.receiptTableView];


+ (UIImage *)snapShotEntireScrollingView:(UIScrollView *)view
{
    return [self snapShotEntireScrollingView:view withEdgeInsets:UIEdgeInsetsZero];
}


+ (UIImage *)snapShotEntireScrollingView:(UIScrollView *)view withEdgeInsets:(UIEdgeInsets)insets
{
    CGRect currentFrame = view.frame;

    CGRect fullFrame = view.frame;
    fullFrame.size.width = MAX(currentFrame.size.width, view.contentSize.width);
    fullFrame.size.height = MAX(currentFrame.size.height, view.contentSize.height);
    view.frame = fullFrame;

    UIImage *image = [self snapShotView:view withEdgeInsets:insets];

    view.frame = currentFrame;
    return image;
}

你的解决方案非常简单明了,但是在我的iPad设备上仍然无法正常工作...最终,我决定使用renderInContext:方法来捕获我的屏幕。我本想使用iOS 7的新方法,但这是不可能的。感谢你的帮助 :) - jaaakub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接