任何人都請幫助我, 我使用此代碼創建圖像動畫滑塊,圖像從webservice獲取,當我通過使用objectatindex加載圖像時:i圖像只加載1,2 ,...沒有加載到第0個索引,我做了什麼錯誤,我不知道,請提前引導我,謝謝,這裏是代碼。從ios中從webservie中加載圖片
-(void)viewDidLoad {
[super viewDidLoad];
NSURL *blogURL = [NSURL URLWithString:@"http://www.example.com/apps/mainslider.php"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSArray *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
data = [dataDictionary valueForKey:@"slider_image"];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] init ];//WithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+40)];//0, 264, 480, 36
[pgCtr setTag:12];
pgCtr.numberOfPages=0;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Skip" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:247/255.0 green:183/255.0 blue:25/255.0 alpha:1]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 490, 160.0, 40.0);
[self.view addSubview:button];
}
-(void)setupScrollView:(UIScrollView*)scrMain {
for (int i=0; i<data.count; i++) {
// create image
image = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://www.example.com/uploads/appbanners/%@",[data objectAtIndex:i]]]]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*[data count], scrMain.frame.size.height)];
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];
}
- (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
// get the current offset (which page is being displayed)
CGFloat contentOffset = scrMain.contentOffset.x;
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.width) + 1 ;
// if page is not 10, display it
if(nextPage!=imagesCount) {
[scrMain scrollRectToVisible:CGRectMake(nextPage*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=nextPage;
} else {
[scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=0;
}
}
你爲什麼要做i-1?我認爲它應該是我唯一的。 –