Introduction
ASIWebThumbnail includes an API you can use to generate web page thumbnails from inside your applications. Rather than embdedding the functionality directly, you embed a version of the command line tool, and use the API to talk to it behind the scenes. The wrapper code uses an NSTask to open the thumbnail generator in a separate process.
There are a couple of advantages to this approach:
- Adobe Flash Player Web content that has been known to crash web browsers will not affect the running of your application
- Webkit is not thread-safe, but using a process per thumbnail, you won't run into any threading issues
How does ASIWebThumbnail work?
ASIWebThumbnail opens a window off the screen, contaning a WebView that will be used to render the page. It then waits for the page to render (timing out when appropriate if you have set it to do so), then grabs an image of the page, resizes it, and saves it to the file specified as a PNG image.
The BundledThumbnailGenerator.app is basically the command line application, wrapped in its own bundle, with LSBackground set in its Info.plist so users won't see the thumbnail generators come up in their dock.
Including ASIWebThumbnail in your application
- Open the ASIWebThumbnail Xcode project, and build the BundledThumbnailGenerator target.
- Copy BundledThumbnailGenerator.app into your project folder
- Expand 'Targets -> Your Application -> Copy Bundle Resources' in the Xcode sidebar
- Drag BundledThumbnailGenerator.app onto Copy Bundle Resources, and perform a 'Make Clean'
- Add ASIWebThumbnailGenerator.h and ASIWebThumbnailGenerator.m to your project
Example usage (synchronous)
ASIWebThumbnailGenerator *generator = [[[ASIWebThumbnailGenerator alloc] init] autorelease];
[generator setUrl:@"http://apple.com"];
[generator setPageSize:NSMakeSize(1000,0)];
[generator setSourceSize:NSMakeSize(1000,700)];
[generator setDestinationSize:NSMakeSize(300,200)];
[generator start];
NSImage *img = [generator image];
Example usage (asynchronous)
In this example, queue is an NSOperationQueue
- (IBAction)generateThumbnail:(id)sender
{
[queue cancelAllOperations];
[queue release];
queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:2];
NSArray *urls = [NSArray arrayWithObjects:
@"http://slashdot.org",
@"http://wired.com",
@"http://news.bbc.co.uk",
@"http://theregister.co.uk",
@"http://github.com",
@"http://adobe.com",
@"http://allseeing-i.com/ASIHTTPRequest",
@"http://stackoverflow.com",
@"http://www.yellow5.com/pokey/archive/index422.html",
nil];
int i=0;
for (NSString *url in urls) {
ASIWebThumbnailGenerator *generator =
[[[ASIWebThumbnailGenerator alloc] init] autorelease];
[generator setUrl:url];
[generator setPageSize:NSMakeSize(1000,0)];
[generator setSourceSize:NSMakeSize(1000,700)];
[generator setDestinationSize:NSMakeSize(300,200)];
[generator setDelegate:self];
[queue addOperation:generator];
i++;
}
}
- (void)thumbnailGenerationSucceededFor:(ASIWebThumbnailGenerator *)generator
{
NSImage *img = [generator image]; //Do something with the image
}