在 ios 中从 webservie 加载图像

Loading images from webservie in ios

任何人都请帮助我, 我正在使用此代码创建图像动画滑块,图像是从网络服务获取的,当我使用 objectatindex:i 加载图像时,图像仅加载 1,2,... 未加载到第 0 个索引中,有什么错误我做了,我不知道,请提前指导我,这是代码。

-(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;
}



}

正如@Uttah Sinha 已经指出的那样,您为图像创建 CGRect 的代码似乎是错误的...

// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];

应该是

// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];

您标记 UIImageView 的方式也会遇到问题,当 i 为 1 时,scrollView 和 ImageView 将具有相同的 tag 编号,这也将是如果您有超过 12 张图片,则为真。您可能需要考虑向 ImageView 添加偏移量,例如

imgV.tag=i+100;