【发布时间】:2014-09-04 23:25:53
【问题描述】:
我知道这很常见,但我只是想知道为什么这个非常简单的代码不起作用。
我在 webViewDidFinishLoad 方法内的 NSLog 行中有一个断点,我正在检查生成的 UIImage(使用快速查看)。结果是 UIWebView 应该位于的空白图像(以及出于调试目的我放置在那里的黑色背景)。
#import "KIViewController.h"
@interface KIViewController ()
@end
@implementation KIViewController
-(void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"start");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"webview did finish load");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,640,200)];
wv.delegate = self;
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[wv loadRequest:nsrequest];
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:wv];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
为什么会发生这种情况,我该如何预防?
【问题讨论】:
-
在将请求加载到 Web 视图之前尝试将 Web 视图添加到
self.view。 -
它不起作用@rmaddy。
-
你到底想用 webview/image 做什么?
-
我正在使用完全相同的代码并且它在我的端工作正常..
-
@Mike 我只是想看看 UIImage 是否有 WebView 的内容。以下是我发布的代码得到的结果:dropbox.com/s/r7ror8yjg843ufa/…
标签: ios objective-c uiwebview uiimage