gpt4 book ai didi

ios - 在uiablview中显示多个uiwebview

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:30 25 4
gpt4 key购买 nike

我遇到了 webview 的问题,我想很多人都遇到过。

在我的应用程序中,我有一个 UITableview 并且 tableview 的每个单元格都是自定义的。在每个单元格中嵌入了一个 UIWebview。当我滚动 tableview 时,我需要显示每个单元格的内容。 (webview的loadsHTML和image_url来自本地数组)

问题是每个单元格中的 UIWebview 都加载了一个远程图像 url,当用户快速滚动表格时,单元格中的 webview react 速度不够快,因此 webview 可能会显示重复的图像不到 1 秒。 (因为我使用可重复使用的单元格和 webview 从 image_url 数组加载,所以

image_url = [array_image_url objectAtIndex:[index row]];

对于用户体验来说,看到重复的图片是很糟糕的。我试图弄清楚,但仍然不能。谁能帮我解决这个问题?

PS:如果可能的话,我不想在磁盘上缓存图像

最佳答案

我遇到过类似的问题(我正在显示远程图像)并使用 UIScrollView 委托(delegate)方法(UITableView 继承自 UIScrollView)来处理何时更新我的​​图像。您可以在滚动停止时执行类似于加载内容的操作:

// UIScrollView Delegate Methods
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
[self loadImagesForOnscreenRows];
}
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self loadImagesForOnscreenRows];
}

编辑 - 忘记了实际加载行的方法。这可能有帮助:)注意 - 此代码中的一些特定于我的应用程序,您需要进行调整以适合您的数据和图像的 Web 位置。此外 - setImageWithURL 方法是 AFNetworking 库的一部分。如果您还没有检查过 AFNetworking,它会很震撼。如果您还没有使用 AFN 并且不需要,那么我还添加了一个我编写的类,它使用 block 来异步加载网络图像。

- (void)loadImagesForOnscreenRows
{
if ([self.phonebookData count] > 0)
{
NSArray *visiblePaths = [self.myTableView indexPathsForVisibleRows];
for (NSIndexPath *indexPath in visiblePaths)
{
NSDictionary *selRow = [[self.persons valueForKey:[[[self.persons allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

PBCell *cell = (PBCell *)[self.myTableView cellForRowAtIndexPath:indexPath];

if ([[selRow objectForKey:@"picFileName"] length] > 0) {
[cell.thumbnailImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"YourURLHere/%@",[selRow objectForKey:@"picFileName"]]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

} else {
cell.thumbnailImage.image = [UIImage imageNamed:@"defaultImage.png"];
}
}
}
}

替代 AFNetworking 的方法:

WebImageOperations.h:

//
// WebImageOperations.h
//
// Created by Larry Wilson on 11/11/11.
// Copyright (c) 2011 Larry Wilson. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface WebImageOperations : NSObject {
}

+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage;

@end

WebImageOperations.m:

//
// WebImageOperations.m
//
// Created by Larry Wilson on 11/11/11.
// Copyright (c) 2011 Larry Wilson. All rights reserved.
//

#import "WebImageOperations.h"
#import <QuartzCore/QuartzCore.h>

@implementation WebImageOperations

+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage
{
NSURL *url = [NSURL URLWithString:urlString];

dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("com.myappname.processimagedataqueue", NULL);
dispatch_async(downloadQueue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:url];

dispatch_async(callerQueue, ^{
processImage(imageData);
});
});
dispatch_release(downloadQueue);
}

@end

关于ios - 在uiablview中显示多个uiwebview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11968810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com