Created
February 15, 2010 16:55
-
-
Save soffes/304786 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // UIWebView+awesomeness.h | |
| // TWToolkit | |
| // | |
| // Created by Sam Soffes on 6/22/09. | |
| // Copyright 2009 Tasteful Works, Inc. All rights reserved. | |
| // | |
| @interface UIWebView (awesomeness) | |
| - (UIScrollView *)scroller; | |
| - (UIView *)contentView; | |
| - (CGPoint)scrollOffset; | |
| - (UIImage *)imageRepresentation; | |
| - (UIImage *)croppedImageRepresentation; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // UIWebView+awesomeness.m | |
| // TWToolkit | |
| // | |
| // Created by Sam Soffes on 6/22/09. | |
| // Copyright 2009 Tasteful Works, Inc. All rights reserved. | |
| // | |
| #import "UIWebView+awesomeness.h" | |
| #import "UIImage+crop.h" | |
| #import <QuartzCore/QuartzCore.h> | |
| @implementation UIWebView (awesomeness) | |
| - (UIScrollView *)scroller { | |
| // This is actually a UIScroller not a UIScrollView | |
| return (UIScrollView *)[[self subviews] objectAtIndex:0]; | |
| } | |
| - (UIView *)contentView { | |
| // Safe guard. It should never return nil | |
| if ([[[self scroller] subviews] count] < 11) { | |
| return nil; | |
| } | |
| return [[[self scroller] subviews] objectAtIndex:10]; | |
| } | |
| - (CGPoint)scrollOffset { | |
| // This isn't completly accurate, but it's pretty close | |
| // It gets less accurate the more you scroll down | |
| NSArray *subviews = [[self scroller] subviews]; | |
| if ([subviews count] < 13) { | |
| return CGPointZero; | |
| } | |
| // 11 is the horizontal scroll indicator | |
| // 12 is the vertical scroll indicator | |
| return CGPointMake([[subviews objectAtIndex:11] frame].origin.x, [[subviews objectAtIndex:12] frame].origin.y); | |
| } | |
| - (UIImage *)imageRepresentation { | |
| UIView *view = [[[self scroller] subviews] objectAtIndex:10]; | |
| UIGraphicsBeginImageContext(view.bounds.size); | |
| [view.layer renderInContext:UIGraphicsGetCurrentContext()]; | |
| UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
| UIGraphicsEndImageContext(); | |
| return image; | |
| } | |
| - (UIImage *)croppedImageRepresentation { | |
| CGPoint offset = [self scrollOffset]; | |
| CGSize currentSize = [[self contentView] frame].size; | |
| return [[self imageRepresentation] cropToRect:CGRectMake(offset.x, offset.y, currentSize.width - offset.x, currentSize.height - offset.y)]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment