Skip to content

Instantly share code, notes, and snippets.

@soffes
Created February 15, 2010 16:55
Show Gist options
  • Select an option

  • Save soffes/304786 to your computer and use it in GitHub Desktop.

Select an option

Save soffes/304786 to your computer and use it in GitHub Desktop.
//
// 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
//
// 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