Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. - (UIImage *)snapshot {
  2. int webViewHeight = [self stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"].floatValue;
  3.  
  4. CGFloat pageHeight = self.frame.size.height;
  5. int pageCount = webViewHeight / pageHeight;
  6. if ((webViewHeight % pageCount) > 0) {
  7. pageCount++;
  8. }
  9.  
  10. for (int i = 0; i < pageCount; i++) {
  11. UIImage *image;
  12. @autoreleasepool {
  13. UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 2.0);
  14. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  15. image = UIGraphicsGetImageFromCurrentImageContext();
  16. UIGraphicsEndImageContext();
  17.  
  18. NSData *imageData = UIImageJPEGRepresentation(image, 0.0);
  19. NSString *imageName = [NSString stringWithFormat:@"image%i.jpg", i];
  20. NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:imageName];
  21. [imageData writeToFile:filePath atomically:YES];
  22.  
  23. CGPoint nextPoint = CGPointMake(0, pageHeight * (i + 1));
  24. [self.scrollView setContentOffset:nextPoint];
  25.  
  26. image = nil;
  27. imageData = nil;
  28. }
  29. }
  30.  
  31. UIImage *completeImage;
  32. @autoreleasepool {
  33. CGSize completeImageSize = CGSizeMake(self.scrollView.contentSize.width * 2, 40000);
  34. UIGraphicsBeginImageContextWithOptions(completeImageSize, YES, 1.0);
  35. for (int i = 0; i < pageCount; i++) {
  36. NSString *imageName = [NSString stringWithFormat:@"image%i.jpg", i];
  37. NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:imageName];
  38. UIImage *pageImage = [UIImage imageWithContentsOfFile:filePath];
  39.  
  40. CGFloat pageImageHeight = pageHeight * 2;
  41. CGPoint imagePoint = CGPointMake(0, pageImageHeight * i);
  42. [pageImage drawAtPoint:imagePoint];
  43.  
  44. pageImage = nil;
  45. }
  46. completeImage = UIGraphicsGetImageFromCurrentImageContext();
  47. UIGraphicsEndImageContext();
  48. }
  49.  
  50. return completeImage;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement