从 URL iphone sdk 将图像加载到 tableView

发布于 2024-12-06 23:28:02 字数 130 浏览 2 评论 0原文

我有 tableView 并需要从 URL 加载图像。我有一个包含图像 URL 的数组,当页面加载时,我需要将所有图像加载到表视图中。请注意,不是来自单个 URL,而是具有具有不同 URL 的数组。我怎样才能实现它?请帮忙

谢谢。

I have tableView and need to load image from URL. I have an array that contains the URLs of images and when the page loads I need to load all the images into the tableview. Note that, not from a single URL, have an array with different URLs. How can I implement that? Please help

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

自此以后,行同陌路 2024-12-13 23:28:02

您可以使用 GCD 在后台线程中加载图像,如下所示:

//get a dispatch queue
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //this will start the image loading in bg
    dispatch_async(concurrentQueue, ^{        
        NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL];

        //this will set the image when loading is finished
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView.image = [UIImage imageWithData:image];
        });
    });

嗨。但你可能需要添加一个dispatch_release(concurrentQueue);确保没有泄漏。 – 弗兰克 8 月 25 日 19:43

You can use GCD to load images in background thread, like this:

//get a dispatch queue
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //this will start the image loading in bg
    dispatch_async(concurrentQueue, ^{        
        NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL];

        //this will set the image when loading is finished
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView.image = [UIImage imageWithData:image];
        });
    });

Hi. But you probably need to add a dispatch_release(concurrentQueue); to be sure no leak. – Franck Aug 25 at 19:43

朦胧时间 2024-12-13 23:28:02

当您想从互联网下载图像时,可以使用延迟加载

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //All you reusable cell implementation here.
   //Since your Images sizes differ. Keep a custom Imageview

    if(![imagesForCategories containsObject:indexPath])
    {    
        customImageView.image = [UIImage imageNamed:@"default-image.png"];
        NSMutableArray *arr = [[NSArray alloc] initWithObjects:[imageUrlArray objectAtIndex:indexPath.row],indexPath, nil];
        [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];
        [arr release];
    }
    return cell;
}

- (void) loadImageInBackground:(NSArray *)urlAndTagReference 
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURL *imgUrl=[[NSURL alloc] initWithString:[urlAndTagReference objectAtIndex:0]];
    NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
    UIImage *img = [UIImage imageWithData:imgData];
    [imgUrl release]; 
    NSMutableArray *arr = [[NSMutableArray alloc ] initWithObjects:img,[urlAndTagReference objectAtIndex:1], nil  ];
    [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
    [arr release];
    [pool release];
}

- (void) assignImageToImageView:(NSMutableArray *)imgAndTagReference
{
    [imagesForCategories addObject:[imgAndTagReference objectAtIndex:1]];
    UITableViewCell *cell = [celebCategoryTableView cellForRowAtIndexPath:[imgAndTagReference objectAtIndex:1]];
    UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:20];
    profilePic.image = [imgAndTagReference objectAtIndex:0];

}

You can use Lazy Loading when you want to download Images from internet

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //All you reusable cell implementation here.
   //Since your Images sizes differ. Keep a custom Imageview

    if(![imagesForCategories containsObject:indexPath])
    {    
        customImageView.image = [UIImage imageNamed:@"default-image.png"];
        NSMutableArray *arr = [[NSArray alloc] initWithObjects:[imageUrlArray objectAtIndex:indexPath.row],indexPath, nil];
        [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];
        [arr release];
    }
    return cell;
}

- (void) loadImageInBackground:(NSArray *)urlAndTagReference 
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURL *imgUrl=[[NSURL alloc] initWithString:[urlAndTagReference objectAtIndex:0]];
    NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
    UIImage *img = [UIImage imageWithData:imgData];
    [imgUrl release]; 
    NSMutableArray *arr = [[NSMutableArray alloc ] initWithObjects:img,[urlAndTagReference objectAtIndex:1], nil  ];
    [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
    [arr release];
    [pool release];
}

- (void) assignImageToImageView:(NSMutableArray *)imgAndTagReference
{
    [imagesForCategories addObject:[imgAndTagReference objectAtIndex:1]];
    UITableViewCell *cell = [celebCategoryTableView cellForRowAtIndexPath:[imgAndTagReference objectAtIndex:1]];
    UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:20];
    profilePic.image = [imgAndTagReference objectAtIndex:0];

}
小镇女孩 2024-12-13 23:28:02

您可以使用 SDWebImage,它可以非常轻松且快速地在 UITableView 中加载图像。
https://github.com/rs/SDWebImage

You can use SDWebImage which permits very easy and speed loading of images in UITableView.
https://github.com/rs/SDWebImage

茶色山野 2024-12-13 23:28:02

试试这个代码,imagearray 包含图像的 url

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [imagearray objectAtIndex:row]]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];


    cell.imageView.image =image;
    return cell;
}

Try this code,imagearray contains urls of image

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [imagearray objectAtIndex:row]]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];


    cell.imageView.image =image;
    return cell;
}
乖乖公主 2024-12-13 23:28:02

您需要创建自定义单元格以进行延迟加载。这将使您能够正确下载图像并且不会冻结。这是如何执行此操作的很好的示例:
异步图像加载

You need to create your custom cell for lazy loading. This will allow you to download images correctly and without freezing. Here is nice example how to do this:
Asynch image loading

清浅ˋ旧时光 2024-12-13 23:28:02

有了afnetworki,这太简单了。

//afnetworking

#import "UIImageView+AFNetworking.h"

 [cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:@"qhacer_logo.png"]];

With afnetworki, it is too easy.

//afnetworking

#import "UIImageView+AFNetworking.h"

 [cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:@"qhacer_logo.png"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文