【问题标题】:iOS webview strange issue / webview is null after operationiOS webview 奇怪问题/webview 操作后为空
【发布时间】:2014-03-14 11:20:35
【问题描述】:

我正在编写一个应用程序来使用数据作为 json 从网站加载图像。有 2 个队列 - 主队列和等待队列。我确定它们工作正常,内容正在正确下载,但是当我在队列之间切换时第一篇文章根本不加载(当我滑动一篇文章并返回到第一篇文章时,它会加载适当地)。我做了 NSLog 我的所有功能。当我滑动到下一张图片时,它看起来像

2014-03-14 12:09:15.019 xx[2509:60b] checkPostTypeAndDisplayPost
2014-03-14 12:09:15.021 xx[2509:60b] displayPostImage
2014-03-14 12:09:15.022 xx[2509:60b] displayPostImage - END
2014-03-14 12:09:15.023 xx[2509:60b] displayTitleAndDescriptionLabel
2014-03-14 12:09:15.024 xx[2509:60b] displayNumberOfPointsAndComments
2014-03-14 12:09:15.025 xx[2509:60b] configureLikeButton
2014-03-14 12:09:15.032 xx[2509:60b] webViewDidStartLoad
2014-03-14 12:09:15.454 xx[2509:60b] webViewDidFinishLoad

但是当我改变队列时,第一个帖子看起来像:

2014-03-14 12:09:15.019 xx[2509:60b] checkPostTypeAndDisplayPost
2014-03-14 12:09:15.021 xx[2509:60b] displayPostImage
2014-03-14 12:09:15.022 xx[2509:60b] displayPostImage - END
2014-03-14 12:09:15.023 xx[2509:60b] displayTitleAndDescriptionLabel
2014-03-14 12:09:15.024 xx[2509:60b] displayNumberOfPointsAndComments
2014-03-14 12:09:15.025 xx[2509:60b] configureLikeButton

我的方法:

-(void)loadPostsAndDisplayPostContent:(void (^)(BOOL finished)) completion{
    [[MyApi client] loadPosts:^(BOOL isSuccess) {
        if(isSuccess){

            // DEBUG
            NSLog(@"loadPostsAndDisplayPostContent");

            _currentPost = [[MyApi client] getCurrentPostFromBundleOfPosts];
            NSLog(@"%@", _currentPost.link);
            [self checkPostTypeAndDisplayPost];
            [self displayTitleAndDesciptionLabel];
            [self displayNumberOfPointsAndComments];
            [self configureLikeButton];
            completion(YES);
        }else{
            completion(NO);
        }
    }];
}

-(void)checkPostTypeAndDisplayPost{

    // DEBUG
    NSLog(@"checkPostTypeAndDisplayPost");

    if ([_currentPost.type isEqualToString:@"image"])
        [self displayPostImage];
    else if ([_currentPost.type isEqualToString:@"movie"])
        [self setUpVideoPlayButton];
}

-(void)displayPostImage{

    // DEBUG
    NSLog(@"displayPostImage");

    [_galleryButton removeFromSuperview];
    [_webView loadHTMLString:
     [UUUHelperMethods configureImageDimensions:_currentPost.image] baseURL:nil];

    // DEBUG
    NSLog(@"displayPostImage - END");
}

#pragma mark swipe recognizer 

- (IBAction)swipeRight:(id)sender {
    _currentPost = [[MyApi client] getPreviousPostFromBundleOfPosts];
    [self checkPostTypeAndDisplayPost];
    [UIWebView animateWithDuration:0.5 animations:^{
        [UIWebView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIWebView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.webView cache:NO];
    }];
    [self displayTitleAndDesciptionLabel];
    [self displayNumberOfPointsAndComments];
    [self configureLikeButton];
}

- (IBAction)swipeLeft:(id)sender {
    _currentPost = [[MyApi client] getNextPostFromBundleOfPosts];
    [self checkPostTypeAndDisplayPost];
    [UIWebView animateWithDuration:0.5 animations:^{
        [UIWebView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIWebView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.webView cache:NO];
    }];
    [self displayTitleAndDesciptionLabel];
    [self displayNumberOfPointsAndComments];
    [self configureLikeButton];
}

#pragma mark webview delegate methods 

-(void)webViewDidStartLoad:(UIWebView *)webView{

    // DEBUG≠
    NSLog(@"webViewDidStartLoad");

    _webView.hidden = YES;
    [SVProgressHUD show];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    // DEBUG
    NSLog(@"webViewDidFinishLoad");

    _webView.hidden = NO;
    [SVProgressHUD dismiss];
}

和HelperMethod配置维度:

+(NSString *)configureImageDimensions:(NSURL*)pictureURL{
    NSString *htmlString = [NSString stringWithFormat:
                            @"<html>"
                            "<head>"
                            "<script type=\"text/javascript\" >"
                            "function display(img){"
                            "var imgOrigH = document.getElementById('image').offsetHeight;"
                            "var imgOrigW = document.getElementById('image').offsetWidth;"
                            "var bodyH = window.innerHeight;"
                            "var bodyW = window.innerWidth;"
                            "if((imgOrigW/imgOrigH) > (bodyW/bodyH))"
                            "{"
                            "document.getElementById('image').style.width = bodyW + 'px';"
                            "document.getElementById('image').style.top = (bodyH - document.getElementById('image').offsetHeight)/2  + 'px';"
                            "}"
                            "else"
                            "{"
                            "document.getElementById('image').style.height = bodyH + 'px';"
                            "document.getElementById('image').style.marginLeft = (bodyW - document.getElementById('image').offsetWidth)/2  + 'px';"
                            "}"
                            "}"
                            "</script>"
                            "</head>"
                            "<body style=\"margin:0;width:100%%; height:100%%;  \" >"
                            "<img id=\"image\" src=\"%@\" onload=\"display()\" style=\"position:relative\" />"
                            "</body>"
                            "</html>",pictureURL
                            ];

    return htmlString;
}

我确信这些方法可以正常工作,因为它适用于 iOS6。经过一些更改并转移到 iOS7 它不起作用。任何想法如何解决它?

EDIT1:我尝试在加载完成后再次调用 Display Image,但它不起作用。

EDIT2:切换队列后我的 webview 为空。知道如何解决吗?

解决方案:我知道,这是旧线程,但也许 some1 也有同样的问题。解决此问题的最佳方法是在主类中添加 NSNotification,而不是从另一个类加载 webViews。我不相信我有多愚蠢。玩得开心!

【问题讨论】:

  • webView:didFailLoadWithError: 有什么有趣的吗?
  • 不,webView:didFailLoadWithError: 没有调用。

标签: ios uiwebview


【解决方案1】:

我推荐使用 Safari 开发者工具,看看加载了哪些内容,控制台中可能会有一些有用的日志

【讨论】:

  • 不,只是Safari工具,要使用它,你需要启用开发者菜单,并将Web检查器连接到模拟器或iDevice中的UIWebView
  • 您可以在displayPostImage 中添加更多日志吗?请添加NSLog(@"webView: %@", _webView);configureImageDimensions NSLog(@"url: %@", pictureURL); 完成后请使用新日志更新您的问题。
  • 我发现了问题。更改队列后 Webview 为空。知道如何解决吗? (图片网址正确)
  • 我很乐意提供帮助),关于 webView,从你发布的代码很难说,显示你在哪里/如何创建 webView?
  • 我的 webView 是属性(CTRL + 拖动)。我在 viewDidLoad 中调用了一些方法,例如 delegate、scrollView 和 scalesPageToFit。我没有手动分配。
猜你喜欢
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 2011-03-14
相关资源
最近更新 更多