Raheel Raheel - 5 months ago 39
Objective-C Question

Show the array of images in an imageview in IOS

I have a view controller which loads an array of images when instantiated. I have a slider used to select which image to show. Now I want to show a custom slider with preview of each images that the user could tap. I have tried some code but whenever i tap the image in the slider it only shows first image in an image view from the array that are coming from server and it does not show the other images from the array. The code i tried is this:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayOfImages = [userDefaults objectForKey:@"property_images"];
NSLog(@"GGG %@",arrayOfImages);

self.imagesData=[[NSArray alloc]init];
self.imagesData = arrayOfImages;
NSLog(@"Image Array is %@",_imagesData);

dispatch_group_t group = dispatch_group_create();
for(int i=0; i<self.imagesData.count;i++){

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame))];
imageView.contentMode = UIViewContentModeScaleAspectFill;
// imageView.image = [UIImage imageNamed:[self.imagesData objectAtIndex:i]];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandImage:)];
tap.numberOfTapsRequired = 1;
tap.view.tag = i;
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer:tap];

[self.scrollView addSubview:imageView];


[imageView sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:i]] placeholderImage:nil options:SDWebImageHighPriority completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {


the expand method is this,

// _fullImage.image = [UIImage imageNamed:[self.imagesData objectAtIndex:recogniser.view.tag]];

[_fullImage sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:recogniser.view.tag]]
placeholderImage:[UIImage imageNamed:@"launch.png"]];
NSLog(@"RRR %@",self.imagesData);
NSLog(@"TTT %ld",recogniser.view.tag);

Answer Source

Set tag on imageView.tag instead of tap.view.tag.

You are setting tap.view.tag when tap.view contains nil value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download