iCarousel显示来自图像阵列的图像'网址

时间:2017-01-05 07:52:38

标签: ios objective-c arrays icarousel

我已经在iPad上使用相关代码为我的横幅视图实现了iCarousel:

- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
    return (NSInteger)[self.bannerDatas count];
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    if (view == nil)
    {

        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 215.0f, 155.0f)];
        ((UIImageView *)view).image = [UIImage imageNamed:[self.netImages objectAtIndex:index]];

        view.contentMode = UIViewContentModeCenter;

    }
    return view;
}

但问题出在这一行:

((UIImageView *)view).image = [UIImage imageNamed:[self.netImages objectAtIndex:index]];

netImages需要是图像的名称才能使其有效,但我的netImages包含图像数组'网址。所以我的问题是如何使用图像URL代替Carousel的名称?

以下是netImages获取数据的方式:

- (NSArray *)setUpNetImages {

    self.netImages = [NSMutableArray new];

    for (BannerData *data in self.bannerDatas) {

        if (data.imageUrl != nil) {
            [self.netImages addObject:data.imageUrl]; //original

        }
    }

    return self.netImages;
}

1 个答案:

答案 0 :(得分:-1)

请使用SDwebImage为您提供从url下载Asynchronous orderer中图像的功能,并在下载完成后更新您的图像。您的UI永远不会停止。

((UIImageView *)view)sd_setImageWithURL:[NSURL URLWithString:[self.netImages objectAtIndex:index]]
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]]

您只需添加并导入

即可
#import <SDWebImage/UIImageView+WebCache.h>