jeudi 4 septembre 2014

Why does this UIWebView to UIImage code render a blank image?


Vote count:

0




I know this is pretty common, but I just wanted to know why this very simple code doesn't work.


I have a breakpoint in the NSLog line inside the webViewDidFinishLoad method and I'm inspecting the resulting UIImage (using quick look). The result is a blank image where the UIWebView should be (and the black background I placed there for debugging purposes).



#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


Why is this happening and how can I prevent it?



asked 10 secs ago

Hal

1,134






Why does this UIWebView to UIImage code render a blank image?

Aucun commentaire:

Enregistrer un commentaire