Create Snapshot Images from Webpages in Cocoa


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // MWWebSnapshot
  2. //
  3. // Created by Jim McGowan on 08/09/2010.
  4. // Copyright 2010 Jim McGowan. All rights reserved.
  5. //
  6. // This code is made available under the BSD license.
  7. // Please see the accompanying license.txt file
  8. // or view the license online at http://www.malkinware.com/developer/License.txt
  9. //
  10.  
  11. #import <Cocoa/Cocoa.h>
  12. #import <WebKit/WebKit.h>
  13.  
  14.  
  15. @interface MWWebSnapshot : NSObject
  16. {
  17. void (^completionBlock)(NSImage *image);
  18. WebView *webView;
  19. }
  20.  
  21. + (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;
  22.  
  23. @end
  24.  
  25.  
  26.  
  27. @interface MWWebSnapshot()
  28. - (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
  29. - (void)_beginDownloadFromURL:(NSURL *)url;
  30. @end
  31.  
  32.  
  33.  
  34. @implementation MWWebSnapshot
  35.  
  36.  
  37. + (void)takeSnapshotOfWebPageAtURL:(NSURL *)url completionBlock:(void (^)(NSImage *))block;
  38. {
  39. MWWebSnapshot *instance = [[self alloc] _initWithCompletionBlock:block];
  40. [instance _beginDownloadFromURL:url];
  41. [instance autorelease];
  42. }
  43.  
  44.  
  45. - (id)_initWithCompletionBlock:(void (^)(NSImage *))block;
  46. {
  47. self = [super init];
  48. if (self != nil)
  49. {
  50. completionBlock = [block copy];
  51.  
  52. webView = [[WebView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 1000.0, 1000.0)
  53. frameName:nil
  54. groupName:nil];
  55. [webView setFrameLoadDelegate:self];
  56. }
  57. return self;
  58. }
  59.  
  60.  
  61. - (void)_beginDownloadFromURL:(NSURL *)url;
  62. {
  63. [self retain];
  64. [webView setMainFrameURL:[url absoluteString]];
  65. }
  66.  
  67.  
  68. - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
  69. {
  70. if (frame != [webView mainFrame])
  71. {
  72. return;
  73. }
  74.  
  75. NSView *webFrameViewDocView = [[[webView mainFrame] frameView] documentView];
  76. NSRect cacheRect = [webFrameViewDocView bounds];
  77.  
  78. NSBitmapImageRep *bitmapRep = [webFrameViewDocView bitmapImageRepForCachingDisplayInRect:cacheRect];
  79. [webFrameViewDocView cacheDisplayInRect:cacheRect toBitmapImageRep:bitmapRep];
  80.  
  81. NSSize imgSize = cacheRect.size;
  82. if (imgSize.height > imgSize.width)
  83. {
  84. imgSize.height = imgSize.width;
  85. }
  86.  
  87. NSRect srcRect = NSZeroRect;
  88. srcRect.size = imgSize;
  89. srcRect.origin.y = cacheRect.size.height - imgSize.height;
  90.  
  91. NSRect destRect = NSZeroRect;
  92. destRect.size = imgSize;
  93.  
  94. NSImage *webImage = [[[NSImage alloc] initWithSize:imgSize] autorelease];
  95. [webImage lockFocus];
  96. [bitmapRep drawInRect:destRect
  97. fromRect:srcRect
  98. operation:NSCompositeCopy
  99. fraction:1.0
  100. respectFlipped:YES
  101. hints:nil];
  102. [webImage unlockFocus];
  103.  
  104. NSSize defaultDisplaySize;
  105. defaultDisplaySize.height = 64.0 * (imgSize.height / imgSize.width);
  106. defaultDisplaySize.width = 64.0;
  107. [webImage setSize:defaultDisplaySize];
  108.  
  109. completionBlock(webImage);
  110.  
  111. [self autorelease];
  112. }
  113.  
  114.  
  115. - (void)dealloc
  116. {
  117. [completionBlock release];
  118. [webView release];
  119.  
  120. [super dealloc];
  121. }
  122.  
  123. @end

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.