Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert HTML Content into Image to Show WebView in ObjectiveC iOS App

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 1.96k
    Comment on it

    In our application sometimes there is a need to show webView content as a preview or as a thumbnail and for that simplest solution is to convert html content into image which can then easily be used in our app.

     

     

    So let's see how to do that:

    In case only html string is supplied to your app than its must to first load your content in webView.

    NSString *htmlStr = @"<html><body><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p></body></html>";
    
    [self.myWebView loadHTMLString:htmlStr baseURL:nil]; // load your content in webView

     

    Add line of code for converting webPage content into image in webViewDidFinishLoad method which get called after a web view finishes loading a frame.

     

    Here we are calling sizeToFit: after the webview loaded so that all the content get fit into the image.

    - (void)webViewDidFinishLoad:(UIWebView *)webView{
        
        [webView sizeToFit]; // to get the exact size of webview content
        UIGraphicsBeginImageContext(webView.frame.size);
        
        [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
        
        UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext(); // return webView content image
        
        UIGraphicsEndImageContext();
    }

     

    Convert HTML Content into Image to Show WebView in ObjectiveC iOS App

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: