Search code examples
iosobjective-carraysicarousel

iCarousel display images from array of images' URLs


I have implemented iCarousel for my Banner View on iPad with this relevant codes:

- (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;
}

But the problem lies here at this line:

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

netImages need to be image's name in order to get this works but my netImages contain array of images' URLs. So my question is how to use image URL instead of name for Carousel?

Here below is how netImages get data:

- (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;
}

Solution

  • Please Use SDwebImage that provide you functionality to download your image in Asynchronous orderer from url and update your image once downloading done.and your UI never get halt.

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

    You just need to add it and import

    #import <SDWebImage/UIImageView+WebCache.h>