我想将滚动视图的屏幕外内容渲染为图像。
但是下面的代码只在屏幕上呈现内容,即使它应该呈现整个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();
答案 0 :(得分:5)
原来我的自动调整设置不正确,因此滚动视图的下半部分没有移动......
以下代码效果很好:
代码:
- (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);
}
答案 1 :(得分:0)
嗯,你可以尝试不同的方法。
像现在一样创建上下文(真正高)并拥有currentOffset
将您的scrollView对齐到上下文的顶部,然后渲染一个屏幕。
将contentOffset.y更改为scrollView的高度
通过scrollView height
所以你基本上是一次渲染一个页面。
答案 2 :(得分:0)
(IBAction)screenShot:(id)sender {
scrollView.frame = CGRectMake(0,0,320,700);
UIGraphicsBeginImageContext(scrollView.frame.size);
CGContextRef c = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(c,0,60); [self.view.layer renderInContext:c]; UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//保存在相册中或通过以下代码共享图像
NSArray * itemsToShare = @ [viewImage];
UIActivityViewController * activity = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activity.excludedActivityTypes = @ [];
[self presentViewController:活动动画:YES完成:nil]; }