0

I found this question and the answer is perfect. But what if I want so set the size myself? How do I change the scrollView.contentOffset for this?

For Example I want to call it like this:

#define multiplier 3.0f

CGSize size = CGSizeMake(scrollView.frame.size.width*multiplier, scrollView.frame.size.height*multiplier); 
UIImage *backgroungImage = [self renderScrollViewToImageWith:scrollView size:size];

-(UIImage *)renderScrollViewToImageWith:(UIScrollView *)_scrollView size:(CGSize)size
{
    UIScrollView *contentScrollView = _scrollView;

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

    //what do I do here???
    CGPoint offset = contentScrollView.contentOffset;
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y); <-- what do I do here?

    [contentScrollView drawViewHierarchyInRect:CGRectMake(0, 0, size.width, size.height) afterScreenUpdates:YES];
    UIImage *visibleScrollViewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return visibleScrollViewImage;
}
Community
  • 1
  • 1
imstillalive
  • 369
  • 4
  • 14

1 Answers1

0

try this

CGRect croppingRect = viewToTakeScreenShot.bounds;

UIGraphicsBeginImageContextWithOptions(croppingRect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
if (context == NULL) return nil;
CGContextTranslateCTM(context, -croppingRect.origin.x, -croppingRect.origin.y);

[viewToTakeScreenShot layoutIfNeeded];
[viewToTakeScreenShot.layer renderInContext:context];

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

Try this. It can be help for you.

Mahendra
  • 8,448
  • 3
  • 33
  • 56
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98