【发布时间】: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: 没有调用。