简体   繁体   English

平滑滚动UITableView iPhone,带有图像

[英]smooth scrolling UITableView iPhone, with images

EDIT: after reading the answers below and looking at all the guides i am now lost, i am too noob to figure it out. 编辑:阅读下面的答案并查看了我现在迷路的所有指南之后,我实在是个菜鸟而已。

i don't want the coding done for me, i need some clear cut advice on how to set up a seperate thread and then reference it to my tableView. 我不想为我完成编码,我需要一些关于如何设置单独线程,然后将其引用到tableView的明确建议。

Any tutorials for a NOOB?!? 任何NOOB教程?!?


this is the code i have set up for putting images into my tableView. 这是我为将图像放入tableView设置的代码。 All the images load but only when scrolling through the table. 所有图像均加载,但仅在滚动表时加载。

How can this be stopped? 如何停止呢?

Any help would be appreciated. 任何帮助,将不胜感激。

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];
                        });

Loading the images on a background thread is one option. 将图像加载到后台线程是一种选择。 The better alternative would be to use NSOperationQueue that autmagically handles the background threads. 更好的替代方法是使用自动处理后台线程的NSOperationQueue。

Apple has provided a sample for the same. 苹果公司提供了同样的样品。

Please have a look at Lazy Loading Of TableViewCells 请看一下TableViewCells的延迟加载

I hope it helps :) 希望对您有所帮助:)

EDIT: If you dont know, or dont want to use threads, then there is another alternate for that. 编辑:如果您不知道或不想使用线程,那么还有另一种选择。 Check this out: downloading-images-for-table-without-threads 看看这个: 没有线程的表的下载图像

How big are your images? 您的图片有多大? You may want to scale them down before putting them in the tableView. 您可能需要先缩小它们的大小,然后再将它们放入tableView中。

something like: 就像是:

    #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();

Also, I note that every time your image is in the dictionary, you'll be storing again; 另外,我注意到每次图像在词典中时,您都将再次存储; you probably want to put the caching line back in the (!photo.image) clause. 您可能想将缓存行放回(!photo.image)子句中。

Download the images in a background thread, and when the images become available set them into the UIImageView in the table view cell. 在后台线程中下载图像,并在图像可用时将其设置到表视图单元格中的UIImageView中。 You're stalling the main thread with loading your images over the network. 您正在通过网络加载图像而使主线程停滞不前。

[SOLVED] i added an if statement, it's not optimal but it only lags if the picture doesn't exist. [已解决]我添加了一个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];
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM