在iOS中生成网站屏幕截图

4
作为问题所述,在iOS设备上,如果用户在uitextfield中输入URL,我想生成此URL的缩略图。如何实现这一点?不幸的是,我不知道从哪里开始。
当然,我可以通过将此URL发送到后端并从那里获取图像截图来完成此操作。我知道如何使用python或php之类的东西做这种事情。但我想避免向我的后端服务器发送额外的往返。
我想要一个类似于 - (uiimage *)screenCapture:(nsurl *)url 的东西。

你熟悉OpenGL ES吗? - Matisse VerDuyn
你很可能需要在服务器上完成这个任务,因为在iOS上你必须使用Webkit,而苹果不允许在他们的AppStore应用中访问它。 - Otium
也许这可以帮助你:http://stackoverflow.com/questions/5469093/how-to-create-uiimage-from-uiwebview - hburde
1个回答

2
根据您的问题,我认为您想在生成的屏幕截图之后进行操作。您需要通过编程方式添加一个UIWebView来加载所需的URL,然后对加载的网页进行快照,最后移除UIWebView。以下是步骤:
  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 ;)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接