Skip to content

Instantly share code, notes, and snippets.

@chrisjdavis
Last active November 30, 2016 04:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chrisjdavis/8438360 to your computer and use it in GitHub Desktop.
Save chrisjdavis/8438360 to your computer and use it in GitHub Desktop.
Creating windows offscreen for fun and profit.
- (IBAction)getImageFromWeb:(id)sender {
// grab the width and height of the document in our mobileView.
CGSize contentSize = CGSizeMake(
[[mobileView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"] floatValue],
[[mobileView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]
);
// create a new window, offscreen.
NSWindow *hiddenWindow = [[NSWindow alloc] initWithContentRect: NSMakeRect( -1000,-1000, contentSize.width, contentSize.height )
styleMask: NSTitledWindowMask | NSClosableWindowMask backing:NSBackingStoreNonretained defer:NO];
// grab the dimensions of the viewport of the mobileView, and create a frame for our WebView that matches the dimensions of the document that is loaded.
NSView *viewport = [[[mobileView mainFrame] frameView] documentView]; // width/height of html page
NSRect viewportBounds = [viewport bounds];
NSRect frame = NSMakeRect(0.0, 0.0, contentSize.width, contentSize.height);
// create a new web view to attach to our hidden window.
WebView *hiddenWebView = [[WebView alloc] initWithFrame:frame frameName:@"Hidden.Frame" groupName:nil];
// grab the value of textField as a string
NSString *hURL = [textField stringValue];
// set hiddenWebView as the content of hiddenWindow
[hiddenWindow setContentView:hiddenWebView];
// call loadRequest on hiddenWebView, converting our hURL string to a URLRequest object.
[[hiddenWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:hURL]]];
// we lock our focus on the WebView. This becomes important later when we want to capture this WebView.
[hiddenWebView lockFocus];
// as long as the URL is loading, stick right here.
while ([hiddenWebView isLoading]) {
[hiddenWebView setNeedsDisplay:NO];
[NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate dateWithTimeIntervalSinceNow:1.0] inMode:NSDefaultRunLoopMode dequeue:YES];
}
// once the loading condition is satisfied we can move on.
[hiddenWebView setNeedsDisplay:YES];
// the first step is to create a bitmap (referenced internally as bitmap) that corresponds to the dimensions of our WebView.
// you can see now why we locked focus on the WebView, as we are creating this bitmap by telling it to look at the FocusedView.
bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:viewportBounds];
// now that we have grabbed our image, lets release the focus on the WebView.
[h iddenWebView unlockFocus];
// convert our bitmap to an image for previewing.
NSImage *dispImage = [[NSImage alloc] initWithData:[bitmap TIFFRepresentation]];
// Display the image by assigning it to an NSImageView.
[imagePreview setImage:dispImage];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment