2017-07-28 48 views
0

我在我的项目中使用了slider from this将图像数组加载到IOS中的滑块中

在此滑块中,它们使用静态图像传递给滑块,并且这些图像在滑块中可见。但我有来自服务的图像数组,我将这个数组传递给滑块,滑块工作正常,但它不显示滑块中的图像。我很困惑,为什么它不显示它。我对滑块代码,

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


//self.imagesData=[[NSArray alloc]init]; 
self.imagesData = arrayOfImages; //[_Image1 mutableCopy]; //@[@"image1.jpg", @"image2.jpg", @"image3.jpg"]; 
NSLog(@"Image Array is %@",_imagesData); 
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]]; 
    [self.scrollView addSubview:imageView]; 
} 
self.scrollView.delegate = self; 
index=0; 


// Progammatically init a TAPageControl with a custom dot view. 
self.customPageControl2 = [[TAPageControl alloc] initWithFrame:CGRectMake(20,self.scrollView.frame.origin.y+self.scrollView.frame.size.height,self.scrollView.frame.size.width,40)];//CGRectMake(0, CGRectGetMaxY(self.scrollView.frame) - 100, CGRectGetWidth(self.scrollView.frame), 40) 
// Example for touch bullet event 
self.customPageControl2.delegate  = self; 
self.customPageControl2.numberOfPages = self.imagesData.count; 
self.customPageControl2.dotSize  = CGSizeMake(20, 20); 
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame) * self.imagesData.count, CGRectGetHeight(self.scrollView.frame)); 
[self.view addSubview:self.customPageControl2]; 

和滑块的进一步功能是这个,

-(void)viewDidAppear:(BOOL)animated{ 
     timer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(runImages) userInfo:nil repeats:YES]; 
    } 

-(void)viewDidDisappear:(BOOL)animated{ 
    if (timer) { 
     [timer invalidate]; 
     timer=nil; 
    } 
} 



-(void)runImages{ 
     self.customPageControl2.currentPage=index; 
     if (index==self.imagesData.count-1) { 
      index=0; 
     }else{ 
      index++; 
     } 
     [self TAPageControl:self.customPageControl2 didSelectPageAtIndex:index]; 
    } 

     - (void)viewDidLayoutSubviews 
    { 
     [super viewDidLayoutSubviews]; 


    } 

     - (void)scrollViewDidScroll:(UIScrollView *)scrollView 
    { 
     NSInteger pageIndex = scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame); 
     self.customPageControl2.currentPage = pageIndex; 
     index=pageIndex; 
    } 
    // Example of use of delegate for second scroll view to respond to bullet touch event 
    - (void)TAPageControl:(TAPageControl *)pageControl didSelectPageAtIndex: 

     (NSInteger)currentIndex 
    { 
     index=currentIndex; 
     [self.scrollView scrollRectToVisible:CGRectMake(CGRectGetWidth(self.view.frame) * currentIndex, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame)) animated:YES]; 
    } 
+0

如果删除计时器代码,您是否看到滑块中的图像?我的意思是,只需添加一张图片,看看它是否在那里,然后从那里继续前进。 –

+0

静态图像显示在屏幕上,但我传递的图像阵列不可见。 @JohnnyRockex –

+0

@HamzaImran为什么你不使用UICollectionView? – Tuhin

回答

1

从服务器尝试SDWebImage库下载图像。

的Objective-C:

#import "UIImageView+WebCache.h" 
// imageView.image = [UIImage imageNamed:[self.imagesData objectAtIndex:i]]; comment this line and replace with bottom line 

[imageView sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:i] 
      placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

整个代码

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 sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:i] 
     placeholderImage:[UIImage imageNamed:@"placeholder.png"]];  
     [self.scrollView addSubview:imageView]; 
    } 

SDWebImage Source

+0

@Hamza Imran包含此arrayOfImages的是什么? – KKRocks

+0

它包含来自服务的图像数组。我想用平和的方式展示它。 @KKRocks –

+0

如何以水平滚动方式显示它们?该阵列不包含固定图像时,用户选择它们时会发生变化。 @KKRocks –

0

这是你的了滚动基本设置:

//data 
NSArray * imageArray = @[@"a.jpg",@"b.jpg",@"c.jpg"]; 

//view 
UIScrollView * scrollView = [UIScrollView new]; 
scrollView.frame = CGRectMake(0, 0, 320, 200); 
[self.view addSubview:scrollView]; 

//add in 
float x = 0; 
float imageWidth = 320; 
for (int n = 0; n < imageArray.count; n++){ 

    UIImageView * iv = [UIImageView new]; 
    iv.frame = CGRectMake(x, 0, imageWidth, 200); 
    iv.image = [UIImage imageNamed:imageArray[n]]; 
    iv.contentMode = UIViewContentModeScaleAspectFill; 
    iv.clipsToBounds = true; 
    [scrollView addSubview:iv]; 

    x+=imageWidth; 
} 

//update content size 
scrollView.contentSize = CGSizeMake(x, 200);