我在项目中使用了slider from this。
在此滑块中,他们使用静态图像传递到滑块,这些图像在滑块中可见。但我有来自服务的图像数组,我将此数组传递给滑块,滑块工作正常,但它不会在滑块中显示图像。我很困惑,为什么它没有显示它。我的滑块代码是,
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 :(得分:1)
尝试使用SDWebImage Library从服务器下载图像。
目标-C:
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];
}
整个代码
{{1}}
答案 1 :(得分:0)
这是scrollView的基本设置:
//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);