Return to Snippet

Revision: 31597
at September 9, 2010 07:52 by jimmcgowan


Updated Code
//  MWWebSnapshot
//
//  Created by Jim McGowan on 08/09/2010.
//  Copyright 2010 Jim McGowan. All rights reserved.
//
//  This code is made available under the BSD license.  
//  Please see the accompanying license.txt file
//  or view the license online at http://www.malkinware.com/developer/License.txt
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>


@interface MWWebSnapshot : NSObject 
{
	void (^completionBlock)(NSImage *image);
	WebView *webView;
}

+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;

@end



@interface MWWebSnapshot()
- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
- (void)_beginDownloadFromURL:(NSURL *)url;
@end



@implementation MWWebSnapshot


+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;
{
	MWWebSnapshot *instance = [[self alloc] _initWithCompletionBlock:block];
	[instance _beginDownloadFromURL:url];
	[instance autorelease];
}


- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
{
	self = [super init];
	if (self != nil)
	{
		completionBlock = [block copy];
		
		webView = [[WebView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 1000.0, 1000.0) 
                                               frameName:nil 
                                               groupName:nil];
		[webView setFrameLoadDelegate:self];
	}
	return self;
}


- (void)_beginDownloadFromURL:(NSURL *)url;
{
	[self retain];
	[webView setMainFrameURL:[url absoluteString]];
}


- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
	if (frame != [webView mainFrame])
	{
		return;
	}
	
	NSView *webFrameViewDocView = [[[webView mainFrame] frameView] documentView];
	NSRect cacheRect = [webFrameViewDocView bounds];
	
	NSBitmapImageRep *bitmapRep = [webFrameViewDocView bitmapImageRepForCachingDisplayInRect:cacheRect];
	[webFrameViewDocView cacheDisplayInRect:cacheRect toBitmapImageRep:bitmapRep];
	
	NSSize imgSize = cacheRect.size;
	if (imgSize.height > imgSize.width)
	{
		imgSize.height = imgSize.width;
	}
	
	NSRect srcRect = NSZeroRect;
	srcRect.size = imgSize;
	srcRect.origin.y = cacheRect.size.height - imgSize.height;
	
	NSRect destRect = NSZeroRect;
	destRect.size = imgSize;
	
	NSImage *webImage = [[[NSImage alloc] initWithSize:imgSize] autorelease];
	[webImage lockFocus];
	[bitmapRep drawInRect:destRect 
                     fromRect:srcRect 
                    operation:NSCompositeCopy 
                     fraction:1.0 
               respectFlipped:YES 
                        hints:nil];
	[webImage unlockFocus];
	
	NSSize defaultDisplaySize;
	defaultDisplaySize.height = 64.0 * (imgSize.height / imgSize.width);
	defaultDisplaySize.width = 64.0;
	[webImage setSize:defaultDisplaySize];
	
	completionBlock(webImage);
	
	[self autorelease];
}


- (void)dealloc
{
	[completionBlock release];
	[webView release];
	
	[super dealloc];
}

@end

Revision: 31596
at September 9, 2010 07:52 by jimmcgowan


Updated Code
//
//  MWWebSnapshot.h
//
//  Created by Jim McGowan on 08/09/2010.
//  Copyright 2010 Jim McGowan. All rights reserved.
//
//  This code is made available under the BSD license.  
//  Please see the accompanying license.txt file
//  or view the license online at http://www.malkinware.com/developer/License.txt
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>


@interface MWWebSnapshot : NSObject 
{
	void (^completionBlock)(NSImage *image);
	WebView *webView;
}

+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;

@end



@interface MWWebSnapshot()
- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
- (void)_beginDownloadFromURL:(NSURL *)url;
@end



@implementation MWWebSnapshot


+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;
{
	MWWebSnapshot *instance = [[self alloc] _initWithCompletionBlock:block];
	[instance _beginDownloadFromURL:url];
	[instance autorelease];
}


- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
{
	self = [super init];
	if (self != nil)
	{
		completionBlock = [block copy];
		
		webView = [[WebView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 1000.0, 1000.0) 
                                               frameName:nil 
                                               groupName:nil];
		[webView setFrameLoadDelegate:self];
	}
	return self;
}


- (void)_beginDownloadFromURL:(NSURL *)url;
{
	[self retain];
	[webView setMainFrameURL:[url absoluteString]];
}


- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
	if (frame != [webView mainFrame])
	{
		return;
	}
	
	NSView *webFrameViewDocView = [[[webView mainFrame] frameView] documentView];
	NSRect cacheRect = [webFrameViewDocView bounds];
	
	NSBitmapImageRep *bitmapRep = [webFrameViewDocView bitmapImageRepForCachingDisplayInRect:cacheRect];
	[webFrameViewDocView cacheDisplayInRect:cacheRect toBitmapImageRep:bitmapRep];
	
	NSSize imgSize = cacheRect.size;
	if (imgSize.height > imgSize.width)
	{
		imgSize.height = imgSize.width;
	}
	
	NSRect srcRect = NSZeroRect;
	srcRect.size = imgSize;
	srcRect.origin.y = cacheRect.size.height - imgSize.height;
	
	NSRect destRect = NSZeroRect;
	destRect.size = imgSize;
	
	NSImage *webImage = [[[NSImage alloc] initWithSize:imgSize] autorelease];
	[webImage lockFocus];
	[bitmapRep drawInRect:destRect 
                     fromRect:srcRect 
                    operation:NSCompositeCopy 
                     fraction:1.0 
               respectFlipped:YES 
                        hints:nil];
	[webImage unlockFocus];
	
	NSSize defaultDisplaySize;
	defaultDisplaySize.height = 64.0 * (imgSize.height / imgSize.width);
	defaultDisplaySize.width = 64.0;
	[webImage setSize:defaultDisplaySize];
	
	completionBlock(webImage);
	
	[self autorelease];
}


- (void)dealloc
{
	[completionBlock release];
	[webView release];
	
	[super dealloc];
}

@end

Revision: 31595
at September 9, 2010 07:48 by jimmcgowan


Updated Code
//
//  MWWebSnapshot.h
//
//  Created by Jim McGowan on 08/09/2010.
//  Copyright 2010 Jim McGowan. All rights reserved.
//
//  This code is made available under the BSD license.  
//  Please see the accompanying license.txt file
//  or view the license online at http://www.malkinware.com/developer/License.txt
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>


@interface MWWebSnapshot : NSObject 
{
	void (^completionBlock)(NSImage *image);
	WebView *webView;
}

+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;

@end



@interface MWWebSnapshot()
- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
- (void)_beginDownloadFromURL:(NSURL *)url;
@end



@implementation MWWebSnapshot


+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;
{
	MWWebSnapshot *instance = [[self alloc] _initWithCompletionBlock:block];
	[instance _beginDownloadFromURL:url];
	[instance autorelease];
}


- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
{
	self = [super init];
	if (self != nil)
	{
		completionBlock = [block copy];
		
		webView = [[WebView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 1000.0, 1000.0) frameName:nil groupName:nil];
		[webView setFrameLoadDelegate:self];
	}
	return self;
}


- (void)_beginDownloadFromURL:(NSURL *)url;
{
	[self retain];
	[webView setMainFrameURL:[url absoluteString]];
}


- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
	if (frame != [webView mainFrame])
	{
		return;
	}
	
	NSView *webFrameViewDocView = [[[webView mainFrame] frameView] documentView];
	NSRect cacheRect = [webFrameViewDocView bounds];
	
	NSBitmapImageRep *bitmapRep = [webFrameViewDocView bitmapImageRepForCachingDisplayInRect:cacheRect];
	[webFrameViewDocView cacheDisplayInRect:cacheRect toBitmapImageRep:bitmapRep];
	
	NSSize imgSize = cacheRect.size;
	if (imgSize.height > imgSize.width)
	{
		imgSize.height = imgSize.width;
	}
	
	NSRect srcRect = NSZeroRect;
	srcRect.size = imgSize;
	srcRect.origin.y = cacheRect.size.height - imgSize.height;
	
	NSRect destRect = NSZeroRect;
	destRect.size = imgSize;
	
	NSImage *webImage = [[[NSImage alloc] initWithSize:imgSize] autorelease];
	[webImage lockFocus];
	[bitmapRep drawInRect:destRect fromRect:srcRect operation:NSCompositeCopy fraction:1.0 respectFlipped:YES hints:nil];
	[webImage unlockFocus];
	
	NSSize defaultDisplaySize;
	defaultDisplaySize.height = 64.0 * (imgSize.height / imgSize.width);
	defaultDisplaySize.width = 64.0;
	[webImage setSize:defaultDisplaySize];
	
	completionBlock(webImage);
	
	[self autorelease];
}


- (void)dealloc
{
	[completionBlock release];
	[webView release];
	
	[super dealloc];
}

@end

Revision: 31594
at September 9, 2010 07:41 by jimmcgowan


Updated Code
//
//  MWWebSnapshot.h
//
//  Created by Jim McGowan on 08/09/2010.
//  Copyright 2010 Jim McGowan. All rights reserved.
//
//  This code is made available under the BSD license.  Please see the accompanying license.txt file
//	or view the license online at http://www.malkinware.com/developer/License.txt
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>


@interface MWWebSnapshot : NSObject 
{
	void (^completionBlock)(NSImage *image);
	WebView *webView;
}

+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;

@end



@interface MWWebSnapshot()
- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
- (void)_beginDownloadFromURL:(NSURL *)url;
@end



@implementation MWWebSnapshot


+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;
{
	MWWebSnapshot *instance = [[self alloc] _initWithCompletionBlock:block];
	[instance _beginDownloadFromURL:url];
	[instance autorelease];
}


- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
{
	self = [super init];
	if (self != nil)
	{
		completionBlock = [block copy];
		
		webView = [[WebView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 1000.0, 1000.0) frameName:nil groupName:nil];
		[webView setFrameLoadDelegate:self];
	}
	return self;
}


- (void)_beginDownloadFromURL:(NSURL *)url;
{
	[self retain];
	[webView setMainFrameURL:[url absoluteString]];
}


- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
	if (frame != [webView mainFrame])
	{
		return;
	}
	
	NSView *webFrameViewDocView = [[[webView mainFrame] frameView] documentView];
	NSRect cacheRect = [webFrameViewDocView bounds];
	
	NSBitmapImageRep *bitmapRep = [webFrameViewDocView bitmapImageRepForCachingDisplayInRect:cacheRect];
	[webFrameViewDocView cacheDisplayInRect:cacheRect toBitmapImageRep:bitmapRep];
	
	NSSize imgSize = cacheRect.size;
	if (imgSize.height > imgSize.width)
	{
		imgSize.height = imgSize.width;
	}
	
	NSRect srcRect = NSZeroRect;
	srcRect.size = imgSize;
	srcRect.origin.y = cacheRect.size.height - imgSize.height;
	
	NSRect destRect = NSZeroRect;
	destRect.size = imgSize;
	
	NSImage *webImage = [[[NSImage alloc] initWithSize:imgSize] autorelease];
	[webImage lockFocus];
	[bitmapRep drawInRect:destRect fromRect:srcRect operation:NSCompositeCopy fraction:1.0 respectFlipped:YES hints:nil];
	[webImage unlockFocus];
	
	NSSize defaultDisplaySize;
	defaultDisplaySize.height = 64.0 * (imgSize.height / imgSize.width);
	defaultDisplaySize.width = 64.0;
	[webImage setSize:defaultDisplaySize];
	
	completionBlock(webImage);
	
	[self autorelease];
}


- (void)dealloc
{
	[completionBlock release];
	[webView release];
	
	[super dealloc];
}

@end

Revision: 31593
at September 9, 2010 07:40 by jimmcgowan


Initial Code
//
//  MWWebSnapshot.h
//
//  Created by Jim McGowan on 08/09/2010.
//  Copyright 2010 Jim McGowan. All rights reserved.
//
//  This code is made available under the BSD license.  Please see the accompanying license.txt file
//	or view the license online at http://www.malkinware.com/developer/License.txt
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>


@interface MWWebSnapshot : NSObject 
{
	void (^completionBlock)(NSImage *image);
	WebView *webView;
}

+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;


@end



@interface MWWebSnapshot()
- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
- (void)_beginDownloadFromURL:(NSURL *)url;
@end

@implementation MWWebSnapshot


+ (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;
{
	MWWebSnapshot *instance = [[self alloc] _initWithCompletionBlock:block];
	[instance _beginDownloadFromURL:url];
	[instance autorelease];
}


- (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
{
	self = [super init];
	if (self != nil)
	{
		completionBlock = [block copy];
		
		webView = [[WebView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 1000.0, 1000.0) frameName:nil groupName:nil];
		[webView setFrameLoadDelegate:self];
	}
	return self;
}


- (void)_beginDownloadFromURL:(NSURL *)url;
{
	[self retain];
	[webView setMainFrameURL:[url absoluteString]];
}


- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
	if (frame != [webView mainFrame])
	{
		return;
	}
	
	NSView *webFrameViewDocView = [[[webView mainFrame] frameView] documentView];
	NSRect cacheRect = [webFrameViewDocView bounds];
	
	NSBitmapImageRep *bitmapRep = [webFrameViewDocView bitmapImageRepForCachingDisplayInRect:cacheRect];
	[webFrameViewDocView cacheDisplayInRect:cacheRect toBitmapImageRep:bitmapRep];
	
	NSSize imgSize = cacheRect.size;
	if (imgSize.height > imgSize.width)
	{
		imgSize.height = imgSize.width;
	}
	
	NSRect srcRect = NSZeroRect;
	srcRect.size = imgSize;
	srcRect.origin.y = cacheRect.size.height - imgSize.height;
	
	NSRect destRect = NSZeroRect;
	destRect.size = imgSize;
	
	NSImage *webImage = [[[NSImage alloc] initWithSize:imgSize] autorelease];
	[webImage lockFocus];
	[bitmapRep drawInRect:destRect fromRect:srcRect operation:NSCompositeCopy fraction:1.0 respectFlipped:YES hints:nil];
	[webImage unlockFocus];
	
	NSSize defaultDisplaySize;
	defaultDisplaySize.height = 64.0 * (imgSize.height / imgSize.width);
	defaultDisplaySize.width = 64.0;
	[webImage setSize:defaultDisplaySize];
	
	completionBlock(webImage);
	
	[self autorelease];
}


- (void)dealloc
{
	[completionBlock release];
	[webView release];
	
	[super dealloc];
}

@end

Initial URL
http://www.malkinware.com/developer/

Initial Description

                                

Initial Title
Create Snapshot Images from Webpages in Cocoa

Initial Tags

                                

Initial Language
Objective C