2012-06-18 62 views
0

新的開發者在這裏,林。但是我想要做的是不按按鈕,而是在視圖加載時顯示選取器。我似乎無法讓它出現。這是我所做的,我不知道什麼似乎是錯的。謝謝你的幫助。自定義圖像選取器/ UIScrollView中沒有出現在視圖

- (void)addImage:(UIImage *)image { 
    [_images addObject:image]; 
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(120, 120)]]; 
} 

- (void) createScrollView { 
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,200.0f)]; 

    int row = 0; 
    int column = 0; 
    for(int i = 0; i < _thumbs.count; ++i) { 

     UIImage *thumb = [_thumbs objectAtIndex:i]; 
     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(column*140+24, row*150+10, 100, 100); 
     [button setImage:thumb forState:UIControlStateNormal]; 
     [button addTarget:self 
        action:@selector(buttonClicked:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     button.tag = i; 
     [self.view addSubview:view]; 
     [view addSubview:button]; 

     if (column == 6) { 
      column = 0; 
      row++; 
     } else { 
      column++; 
     } 

    } 

    [view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)]; 
} 
- (IBAction)buttonClicked:(id)sender { 
    UIButton *button = (UIButton *)sender; 
} 

- (void)viewDidLoad 
{ 
    [self createScrollView]; 
    _images = [[NSMutableArray alloc] init]; 
    _thumbs = [[NSMutableArray alloc] init]; 

    for(int i = 0; i <= 100; i++) 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDir = [paths objectAtIndex:0]; 

     NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"images%d.png", i]]; 
     NSLog(@"savedImagePath=%@",savedImagePath); 
     if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
      [_images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 

      NSLog(@"file exists"); 
     } 
     NSLog(@"file does not exist"); 
    } 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

回答

0

您正在創建滾動視圖,然後初始化圖像數組,因此它不會出現。

而是使用像 -

- (void)viewDidLoad 
{ 
_images = [[NSMutableArray alloc] init]; 
_thumbs = [[NSMutableArray alloc] init]; 

for(int i = 0; i <= 100; i++) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"images%d.png", i]]; 
    NSLog(@"savedImagePath=%@",savedImagePath); 
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
     [_images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 

     NSLog(@"file exists"); 
    } 
    NSLog(@"file does not exist"); 
} 

[self createScrollView]; 

[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
} 
+0

已經嘗試過,但仍然沒有顯示任何東西。順便說一句,我用它的iPad。 – Bazinga

+0

您是否嘗試過調試,調用createScrollView方法時數組中的值是什麼? – rishi

+0

顯示滾動視圖,但圖像不顯示。 – Bazinga

0

請注意註釋行

[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 

[super viewDidLoad];被稱爲後你應該做任何事情。其次,在您的createScrollView方法中,您將滾動視圖添加到self.view_thumbs.count次(假設您只使用一個滾動視圖,如果添加一次就足夠了)。

所以把一段代碼

[self.view addSubview:view]; 

上面的for循環。

還做按仙人的答案。

希望這可以幫助你。

3

1日在滾動視圖中添加按鈕,並在for循環的最後一個後添加scrolview在self.view。

- (void) createScrollView { 
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,200.0f)]; 

    int row = 0; 
    int column = 0; 
    for(int i = 0; i < _thumbs.count; ++i) { 

     UIImage *thumb = [_thumbs objectAtIndex:i]; 
     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(column*140+24, row*150+10, 100, 100); 
     [button setImage:thumb forState:UIControlStateNormal]; 
     [button addTarget:self 
        action:@selector(buttonClicked:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     button.tag = i; 

     [view addSubview:button]; 

     if (column == 6) { 
      column = 0; 
      row++; 
     } else { 
      column++; 
     } 

    } 

    [view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)]; 

    [self.view addSubview:view]; 
} 

我希望它適合你。