Generate a website screen capture in ios

Go To StackoverFlow.com

4

As the question says, on a iOS device if a user inputs a URL in uitextfield I want to generate a thumbnail capture of this URL. How can this be achieved? Unfortunately I don't have a clue where to start from.

Of course one way I can do this is to send this URL to backend & get the image screenshot from there. I know how one can do this kinda stuff using python or php. But I want to avoid the extra round trip to my backend servers.

I want something like - (uiimage *)screenCapture:(nsurl *)url

2012-04-04 17:37
by Srikar Appalaraju
are you familiar with opengl es - Matisse VerDuyn 2012-04-04 18:26
You will most likely have to do this on a server because on iOS you would have to use webkit which Apple does not allow access to in their AppStore app - Otium 2012-04-04 18:26
Maybe this helps: http://stackoverflow.com/questions/5469093/how-to-create-uiimage-from-uiwebvie - hburde 2012-04-04 20:23


2

As per your question i think you'll want to manipulate the generated screenshot afterwards. You'll add a UIWebView programmatically to load the needed URL then take a snapshot of the loaded web page then remove the UIWebView Here are the steps

  1. a UIImageView to your viewController and connect it to your viewController class

    @property (weak, nonatomic) IBOutlet UIImageView *image_Thumbnail;
    
  2. having the URL in a variable named "webPageUrlString" add the following lines

    UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    webView.backgroundColor = [UIColor redColor];
    NSURL *websiteUrl = [NSURL URLWithString:**webPageUrlString**];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
    [webView loadRequest: urlRequest];
    webView.delegate = self;
    [self.view addSubview:webView];
    
  3. right now you have requested to load the URL but can't render the snapshot unless the web page is fully loaded,so you'll need to wait for the web page loading notification.In order to that you must subscribe to the webView delegate in the header file like that

    @interface ViewController : UIViewController<UIWebViewDelegate>
    
  4. Now you'll implement the "webViewDidFinishLoad" and take the snapshot you want then hide and remove the webView.

    -(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    viewImage = [[UIImageView alloc] initWithFrame:self.view.frame];
    viewImage.backgroundColor = [UIColor greenColor];
    
    UIGraphicsBeginImageContext(webView.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    viewImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [webView setAlpha:0];
    [webView removeFromSuperview];
    
    [self.view addSubview:viewImage];
    

    }

  5. Now you have the screenshot you want in the property called "viewImage" and you can do what ever you want with it ;)

2014-03-16 09:39
by Mohammad Allam
Ads