【问题标题】:smooth scrolling UITableView iPhone, with images平滑滚动 UITableView iPhone,带图像
【发布时间】:2011-02-20 15:44:39
【问题描述】:

编辑: 在阅读了下面的答案并查看了我现在迷路的所有指南之后,我太菜鸟了,无法弄清楚。

我不想为我完成编码,我需要一些关于如何设置单独线程然后将其引用到我的 tableView 的明确建议。

有适合​​新手的教程吗?!?


这是我设置的用于将图像放入 tableView 的代码。所有图像都会加载,但仅在滚动表格时加载。

如何阻止这种情况?

任何帮助将不胜感激。

NSString *userImage = [(Tweet*)[profile objectAtIndex:indexPath.row]  profileImage];
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[(Tweet*)[profile objectAtIndex:indexPath.row]  profileImage]]];          
            dict =[[NSMutableDictionary alloc]init];
                //UIImage *retImage =[dict  objectForKey:(@"%@", userImage)];
                UIImage *retImage =[dict  objectForKey:userImage];
                dispatch_async(dispatch_get_main_queue(), ^{
                        if (!retImage) {
                        UIImage *profileImage=[UIImage imageWithData:imageData];
                        [dict setObject:profileImage forKey:userImage];
        }
                    UIImage *retImage2 =[dict  objectForKey:userImage];
                    photo.image = retImage2;


                        [imageData release];
                        });

【问题讨论】:

  • 您在问一个特定于表格视图的问题,但您没有显示用于填充/创建单元格的代码 - 在大多数情况下,UITableViewCells 设置不当是滚动缓慢的原因(例如使用透明胶片)。您应该引用 tableView:cellForRowAtIndexPath:indexPath 的代码,因为这很可能是一个关键因素。
  • 当我在我的电脑上时,我会复制它

标签: iphone image url uitableview nsmutabledictionary


【解决方案1】:

在后台线程上加载图像是一种选择。更好的选择是使用自动处理后台线程的 NSOperationQueue。

Apple 提供了相同的示例。

请看Lazy Loading Of TableViewCells

希望对你有帮助:)

编辑:如果您不知道,或者不想使用线程,那么还有另一种替代方法。看一下这个: downloading-images-for-table-without-threads

【讨论】:

  • 不幸的是,我是一个菜鸟,2 个月前开始编码。苹果文档对我来说太混乱了。还是谢谢
  • 我提供了“示例代码链接”。这可能有点复杂,但这是优化的方法。
【解决方案2】:

您的图片有多大?您可能希望在将它们放入 tableView 之前缩小它们。

类似:

    #define kImageSize 44 
   // Create a thumbnail version of the image for the oject.
CGSize size = newImage.size;
CGFloat ratio ;
if (size.width > size.height) {
        ratio = kImageSize / size.width;
} else {
    ratio = kImageSize/ size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

if (NULL != UIGraphicsBeginImageContextWithOptions) { //test that function is available
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);  //allow iphone4 to use higher-res
} else {
    UIGraphicsBeginImageContext(rect.size);
}
[newImage drawInRect:rect];
userThumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

另外,我注意到每次您的图像在字典中时,您都会再次存储;您可能希望将缓存行放回 (!photo.image) 子句中。

【讨论】:

  • 缓存行,你能解释一下吗?
  • 现在我看到了代码,再加点。我绝对同意您应该在后台下载(同时放置一个临时图像)。但是,我对您的第一个代码段和“更多代码”之间的关系感到困惑。看起来您每次都在“tableView:cellForRowAtIndexPath:indexPath”中重新下载图像,这将是一场灾难。
  • 最后几点:你的“if (cell)”行应该只是“else”,因为你刚刚在“then”部分完成了分配,所以不需要这样做再次。最后:调用 NSString "image" 令人困惑;我假设你的意思是“imageName”
  • 第一批代码在第二部分下,但以上所有代码都在“tableView:cellForRowAtIndexPath:indexPath”下。我正在调用“photo.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userImage]]];”没有字典,所以我将 nsmutabledictionary 添加到我已有的内容中。所以我需要弄清楚如何在后台下载:-(
  • 我认为这可能会有所帮助? switchonthecode.com/tutorials/…
【解决方案3】:

在后台线程中下载图像,当图像可用时,将它们设置到表格视图单元格中的 UIImageView 中。您正在通过网络加载图像来停止主线程。

【讨论】:

  • 这听起来很有希望,但我该怎么做呢?
【解决方案4】:

[已解决] 我添加了一个 if 语句,它不是最佳的,但它只会在图片不存在时滞后。

                    if (!image) {
                    [activityIndicator startAnimating];
                        UIImage *phoneImage=[UIImage imageWithData:[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:userImage]]];
                        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                                             NSUserDomainMask, YES);
                        NSString *documentsDirectory = [paths objectAtIndex:0];
                        NSString* path = [documentsDirectory stringByAppendingPathComponent:
                                          [NSString stringWithFormat:@"%@:%@", imageName, fileName]  ];
                        NSData* data = UIImagePNGRepresentation(phoneImage);
                        [data writeToFile:path atomically:YES];
                                            }               
                else{
                    photo.image=image;
                    [activityIndicator stopAnimating];
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2012-03-08
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多