0

i want to render the offscreen content of my scrollview to an image as well.

But the following code only renders the content on screen, even though it's supposed to render the whole scrollView content.

Can anyone provide help?

Thanks in advance!

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();
4

3 回答 3

5

Turns out that my autosizing wasn't set properly so that the lower parts of the scroll view didn't move …</p>

Following code works well:

code:

- (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);
    }
于 2012-07-24T19:38:48.293 回答
0

Well, you could try a different approach.

  • create the context as you do now (real tall) and have the currentOffset

  • align your scrollView at the top of the context, and render one screen full.

  • change the contentOffset.y to be the height of the scrollView

  • render the next screenful in the context offset by the scrollView height

So you are essentially rendering a page at a time.

于 2012-07-23T12:20:37.033 回答
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();

    //Save in your photos album or Share the image through following code

    NSArray * itemsToShare = @[viewImage];

    UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];

    activity.excludedActivityTypes = @[];

    [self presentViewController:activity animated:YES completion:nil]; }

于 2014-07-02T08:02:05.253 回答