平滑滚动的iTableView iPhone,带有图像

平滑滚动的iTableView iPhone,带有图像,iphone,image,url,uitableview,nsmutabledictionary,Iphone,Image,Url,Uitableview,Nsmutabledictionary,编辑: 在阅读了下面的答案并查看了所有的指南之后,我现在迷路了,我太笨了,想不出来 我不想为我做的编码,我需要一些明确的建议,如何设置一个单独的线程,然后引用到我的tableView 有关于NOOB的教程吗 这是我为将图像放入tableView而设置的代码。所有图像都会加载,但只有在表格中滚动时才会加载 如何才能阻止这种情况 任何帮助都将不胜感激 NSString *userImage = [(Tweet*)[profile objectAtIndex:indexPath.row] profi

编辑: 在阅读了下面的答案并查看了所有的指南之后,我现在迷路了,我太笨了,想不出来

我不想为我做的编码,我需要一些明确的建议,如何设置一个单独的线程,然后引用到我的tableView

有关于NOOB的教程吗

这是我为将图像放入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];
                        });

你的照片有多大?在将它们放入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之前,您可能需要缩小它们的比例

比如:

    #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子句。

在后台线程中下载图像,当图像可用时,将其设置到表视图单元格中的UIImageView中。通过网络加载图像会使主线程暂停

在后台线程中下载图像,当图像可用时,将其设置到table view单元格中的UIImageView中。通过网络加载图像会使主线程暂停

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

苹果公司也提供了同样的样品

请看一看

我希望这有助于:

编辑:如果您不知道,或者不想使用线程,那么还有另一种替代方法。看看这个:
将图像加载到背景线程是一种选择。更好的选择是使用自动处理后台线程的NSOperationQueue

苹果公司也提供了同样的样品

请看一看

我希望这有助于:

编辑:如果您不知道,或者不想使用线程,那么还有另一种替代方法。看看这个: [已解决] 我添加了一个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];
                }
[已解决] 我添加了一个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];
                }

您提出的是表格视图特定的问题,但没有显示用于填充/创建单元格的代码-在大多数情况下,UITableViewCells的设置不佳是导致滚动速度缓慢(例如使用透明胶片)的原因。您应该引用tableView:CellForRowatineXpath:indexPath的代码,因为这很可能是一个关键因素。当我在我的计算机上时,我会将它复制到整个屏幕上。您正在问一个表视图特定的问题,但您没有显示填充/创建单元格所使用的代码-在大多数情况下,UITableViewCells设置不当是滚动速度慢的原因,例如使用透明胶片。您应该引用tableView:CellForRowatineXpath:indexPath的代码,因为这很可能是一个关键因素。当我在我的计算机上时,我会将它复制到另一个位置。这听起来很有希望,但我该如何做呢?这听起来很有希望,但我该如何做呢?缓存行,你能解释一下吗?现在我看到了代码,还有几点。我绝对同意你应该在后台下载,同时放置一个临时图像。然而,我对第一个代码段和更多代码段之间的关系感到困惑。看起来您每次都在tableView:CellForRowatingIndexPath:indexPath中下载图像,这将是一场灾难。最后,有几个小问题:您的行if cell应该是else,因为您刚刚完成了then部分中的赋值,所以无需再次执行。最后一点:调用NSString图像令人困惑;我假设您的意思是ImageName第一批代码在第二部分下,但上面所有代码都在tableView:CellForRowatineXpath:indexPath下。我正在调用photo.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userImage]];没有字典,所以我在已有的基础上添加了nsmutabledictionary。所以我需要弄清楚如何在后台下载:-我想这可能会有所帮助?缓存行,你能解释一下吗?现在我看到代码了,还有几点。我绝对同意你应该在后台下载,同时放置一个临时图像。然而,我对第一个代码段和更多代码段之间的关系感到困惑。看起来您每次都在tableView:CellForRowatineXpath:indexPath中下载图像,这将是一场灾难
如果单元格应该是else,因为您刚刚完成了then部分中的赋值,所以无需再次执行。最后一点:调用NSString图像令人困惑;我假设您的意思是ImageName第一批代码在第二部分下,但上面所有代码都在tableView:CellForRowatineXpath:indexPath下。我正在调用photo.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userImage]];没有字典,所以我在已有的基础上添加了nsmutabledictionary。所以我需要弄清楚如何在后台下载:-我想这可能会有所帮助?不幸的是,我是一个傻瓜,2个月前开始编码。苹果的文档对我来说太混乱了。无论如何谢谢,我已经提供了“示例代码链接”。这可能有点复杂,但这是一种优化的方式。不幸的是,我是一个Noob,2个月前开始编码。苹果的文档对我来说太混乱了。无论如何谢谢,我已经提供了“示例代码链接”。这可能有点复杂,但这是一种优化的方法。