从ios中的Web服务加载图像

时间:2015-04-07 07:15:28

标签: ios objective-c uiscrollview

有人请帮助我, 我正在使用此代码创建图像动画滑块,图像来自webservice,当我使用objectatindex加载图像时:我加载的图像只有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;
}



}

1 个答案:

答案 0 :(得分: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;