2013-07-14 42 views
0

如何更改此方法以允许图像通过数组而不是颜色进入?因为我有mySQL数据库,它传递的是在xcode中分配为存储在数组中的NSString的url。更改UIScrollview方法以允许使用图像加载视图

想法是每个视图都从mySQL数据库设置了背景图像。

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blueColor], 
           [UIColor greenColor],[UIColor yellowColor] , nil]; 

      for (int i = 0; i < colors.count; i++) { 

       CGRect frame; 
       frame.origin.x = self.scrollView.frame.size.width * i; 
       frame.size = self.scrollView.frame.size; 

       self.scrollView.pagingEnabled = YES; 

       UIView *subview = [[UIView alloc] initWithFrame:frame]; 
       subview.backgroundColor = [colors objectAtIndex:i]; 

       [self.scrollView addSubview:subview]; 
      } 

      self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height); 

    } 

我做了一个尝试(introNames是阶级其中id和图像的名称是从MySQL数据库举行,imageArray在上面的代码中类创建和introNames NSString的被分配给它)。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    for (int i = 0; i < imageArray.count; i++) { 

     introImages *snap = [imageArray objectAtIndex:i]; 

     imageName2 = snap.imageName; 

     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * i; 
     frame.size = self.scrollView.frame.size; 

     self.scrollView.pagingEnabled = YES; 

     UIView *subview = [[UIView alloc] initWithFrame:frame]; 
     UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName2]]; 

     [subview addSubview:backgroundView]; 
     [self.scrollView addSubview:subview]; 
    } 

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height); 

} 

这是工作数据检索系统。

-(void) retrieveData 
{ 
    NSURL *url = [NSURL URLWithString:getDataUrl]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 

    if (data) { 

     jsonConnection = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
     imageArray = [[NSMutableArray alloc]init]; 

     for (int i = 0; i < jsonConnection.count; i++) 
     { 
      NSString *pID = [[jsonConnection objectAtIndex:i] objectForKey:@"id"]; 
      NSString *pName = [[jsonConnection objectAtIndex:i] objectForKey:@"ImagesURL"]; 

      introImages *myImages = [[introImages alloc]initWithimageID:pID andimageName:pName]; 

      [imageArray addObject:myImages]; 
     } 

    } 

    else { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Something is wrong with your internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 

    } 

任何想法将是巨大的:)

+0

我说使用文件名创建字符串,并只做NSArray * colors = [NSArray arrayWithObjects:[UIImage imageNamed:imageName1],[UIImage imageNamed:imageName2],nil];等... – user2277872

+0

正在考虑,但害怕没有灵活性的权力,远程更改图像。 – Evilelement

+0

那么当您尝试使用IntroImages类时发生了什么? – Zen

回答

相关问题