我在尝试从UIWebViews上截图时遇到了一个问题。我需要为我的UIWebView拍摄一些屏幕截图,它可以工作,但屏幕截图不正确,因为它们是在event webViewDidFinishLoad中拍摄的,但当UIWebView未完全加载时,它会调用webViewDidFinishLoad,我的意思是,我在event webViewDidFinishLoad中拍摄屏幕截图,但UIWebView没有正确和完全加载,所以它拍摄了一个屏幕截图,并使其正确,但屏幕截图并不完全正确,因为它触发了事件(webViewDidFinishLoad),但UIWebView没有完全加载。有什么想法吗?
非常感谢
发布于 2013-07-02 19:36:33
您可以尝试使用UIButton显式地截取屏幕截图,而不是在event webViewDidFinishLoad中截图。如果禁用此按钮,则在web视图完全加载其内容后,您可以将此按钮的enabled属性设置为YES。点击按钮即可实现你的代码截图。
编辑:Webview的webViewDidFinishLoad方法在加载完内容后被调用。可能需要一些时间才能在视图上呈现一些图像/内容。如果可能,您可以在webViewDidFinishLoad方法中使用NSTimer来等待足够时间(1分钟),这意味着webview可以完全加载其内容。
NSTimer *timer = NSTimer scheduledTimerWithTimeInterval:60.0目标:自选择器:@选择器(MethodName:) userInfo: nil重复数:否;
在Selector方法中,你可以实现截图的代码。
希望这能对你有所帮助。
发布于 2013-07-03 12:16:00
一位用户在使用活动指示器时遇到了问题,该指示器会在页面加载之前出现,但他们的活动指示器会在主要内容加载后返回,问题在这里是UIWebView not finishing loading?,答案代码如下所示。在代码之后,我将详细介绍如何在您的情况下使用它。
(在下面的代码中,网页真正开始在//show UIActivityIndicator上加载,并真正完成在//hide UIActivityIndicator上加载主要内容(而不是你正在努力处理的额外内容))。
//Define the NSStrings "lastURL" & "currentURL" in the .h file.
//Define the int "falsepositive" in the .h file. (You could use booleans if you want)
//Define your UIWebView's delegate (either in the xib file or in your code)
- (void)webViewDidFinishLoad:(UIWebView *)webView {
lastURL = [[NSString alloc] initWithFormat:@"%@", webView.request.mainDocumentURL];
if (falsepositive != 1) {
NSLog(@"Loaded");
//hide UIActivityIndicator
} else {
NSLog(@"Extra content junk (i.e. advertisements) that the page loaded with javascript has finished loading");
//This method may be a good way to prevent ads from loading hehe, but we won't do that
}
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *requestURL =[request mainDocumentURL];
currentURL = [[NSString alloc] initWithFormat:@"%@", requestURL]; //not sure if "%@" should be used for an NSURL but it worked...
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
if ([currentURL isEqualToString:lastURL]) {
falsepositive = 1;
NSLog(@"The page is loading extra content with javascript or something, ignore this");
} else {
falsepositive = 0;
NSLog(@"Loading");
//show UIActiviyIndicator
}
}当页面加载到iOS中时,会调用webviewdidstart和webviewdidfinish,但不知道主页面/html内容的差异,objective-c会再次调用它来获取额外的内容,如广告或框架。如果我处在你的情况下,我会创建一个BOOL,比如pageIsLoading,并在webviewdidstart中将其设置为true,然后在webviewdidfinish中将其设置为false。在webviewdidfinish中关闭BOOL后,我将通过调用一个方法来结束webviewdidfinish,该方法将在短暂的延迟后检查BOOL pageIsLoading ==是否为是,如果是,则什么也不做,因为正在加载更多的内容。如果pageIsloading == no,则必须加载所有内容,现在是拍摄快照的好时机。
https://stackoverflow.com/questions/17424380
复制相似问题