2017-09-26 53 views
0

我有一个视图控制器,它在实例化时加载一组图像。我有一个滑块用于选择要显示的图像。现在我想显示一个自定义滑块,并预览用户可以点击的每个图像。我已经尝试了一些代码,但是每当我点击滑块中的图像时,它只会显示来自服务器的阵列的图像视图中的第一个图像,而不会显示数组中的其他图像。我试过的代码是这样的:在IOS的imageview中显示图像阵列

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

    dispatch_group_enter(group); 

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

    _fullImage.hidden=NO; 
    _fullView.hidden=NO; 
} 

展开方法是这样的,

-(void)expandImage:(UITapGestureRecognizer*)recogniser 
{ 
    // _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); 
} 
+0

请参阅https://stackoverflow.com/questions/24864821/display-array-of-images-in-uiimageview – priyadharshini

+0

其滑块我想只显示一个图像,我点击滑块。 @ R.Mohan – Raheel

+0

移动此行'dispatch_group_enter(group);'在此UIImageView *上方图像= [[UIImageView alloc] initWithFrame:CGRectMake' –

回答

0

imageView.tag而不是tap.view.tag设置标签。

当您设置tap.view.tag时,tap.view包含nil值。

+0

如何做到这一点。 @Lal Krishna – Raheel