2012-06-25 71 views
0

林與我的UISCrollViewUIScrollVIew是在我View2Controller,我的UIScrollView具有圖像,這從我ImagePicker拾取圖像(多/單圖像)進口縮略圖。我把[self createScrollView];放在viewDidAppear,所以在我第一次加載的時候只看到UIScrollView,顯然是因爲我還沒有選擇圖片。所以在我選擇它之後,我沒有更新。但我的images.count正在我的調試器中更新。它不是在視圖中更新,但當我去另一個ViewController然後再次返回到View2Controller,它加載圖像在我的UIScrollView,然後當我再次添加圖像它不更新。爲什麼會這樣?的UIScrollView不加載/更新具有問題正確

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

- (void) createScrollView { 

    [scrollView setNeedsDisplay]; 
    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*60+10, row*60+10, 60, 75); 
     [button setImage:thumb forState:UIControlStateNormal]; 
     [button addTarget:self 
        action:@selector(buttonClicked:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     button.tag = i; 

     [scrollView addSubview:button]; 

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

    } 
    [scrollView setContentSize:CGSizeMake(300, (row+1) * 60 + 10)]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [self createScrollView]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
     self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43, 370, 310, 143)]; 
     CAGradientLayer *gradient = [CAGradientLayer layer]; 
     gradient.frame = self.slotBg.bounds; 
     gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; 
     [self.slotBg.layer insertSublayer:gradient atIndex:0]; 
     [self.view addSubview:self.slotBg]; 

     self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,130.0f)]; 
     [slotBg addSubview:self.scrollView]; 
      for(int i = 0; i <= 100; i++) 
      { 
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
       NSString *documentsDir = [paths objectAtIndex:0]; 

       NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; 
       NSLog(@"savedImagePath=%@",savedImagePath); 
       if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 
        NSLog(@"file exists"); 
       } 
      } 
      NSLog(@"Count : %d", [_images count]); 
} 

編輯:(在這一部分,圖像無法從視圖中刪除,但在我的NSDocumentDirectory被刪除。)

- (IBAction)buttonClicked:(id)sender { 

     _clickedButton = (UIButton *)sender; 
     UIAlertView *saveMessage = [[UIAlertView alloc] initWithTitle:@"" 
                   message:@"DELETE IMAGE?" 
                  delegate:self 
                cancelButtonTitle:@"NO" 
                otherButtonTitles:@"YES", nil]; 
     [saveMessage show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 


    if([title isEqualToString:@"YES"]) { 
     NSLog(@"YES was selected."); 
     UIButton *button = _clickedButton1; 
     [button removeFromSuperview]; 
     [_images1 objectAtIndex:button.tag]; 
     [_images1 removeObjectAtIndex:button.tag]; 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"firstSlotImages%lu.png", button.tag]]; 
     [fileManager removeItemAtPath: fullPath error:NULL]; 
     NSLog(@"image removed");  
    } 
+1

爲什麼當你已經有一個問題時你需要創建一個新問題:同樣的問題? http://stackoverflow.com/questions/11130757/uiscrollview-not-updating-and-displaying-properly – Rog

+0

@ Kobe.o4 - 這是同樣的問題?這是一個單獨的問題,但你拿走了我的答案,因爲我沒有回答你沒有發佈的問題呢?無論如何,'[_images1 objectAtIndex:button.tag]; [_images1 removeObjectAtIndex:button.tag];'這工作? button.tag是否有適當的索引?此外,爲什麼你收集的對象,但不使用它。 ['_images1 removeObject:button]不是更好嗎?''? –

+0

@MichaelBoselowitz,對不起。但是'[_images1 removeObject:button];'在我看來還沒有刪除。 – Bazinga

回答

0

viewDidAppear:一旦你到了只能稱爲視圖。因此,當您「添加」圖像時,只會將它們添加到_images陣列中,因爲您沒有將它們添加到屏幕中,所以不會顯示它們。當您離開視圖並返回時會顯示它們,因爲viewDidAppear:被再次調用。

要解決此問題,請使用addImage:將其添加到當前視圖。

- (void)addImage:(UIImage *)image { 
    int row = ?; 
    int column = ?; 
    [_images addObject:image]; 
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]]; 
    UIImage *thumb = [_thumbs objectAtIndex:_thumbs.count-1]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(column*60+10, row*60+10, 60, 75); 
    [button setImage:thumb 
      forState:UIControlStateNormal]; 
    [button addTarget:self 
       action:@selector(buttonClicked:) 
    forControlEvents:UIControlEventTouchUpInside]; 
    button.tag = _thumbs.count-1; 
    [scrollView addSubview:button]; 
} 

唯一的問題當然是如何確定位置,又名列和行。您可以通過獲取最後一個大拇指的框架並根據屏幕邊界確定將其放置在何處。

另一個修復

- (void)addImage:(UIImage *)image { 
    [_images addObject:image]; 
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]]; 
    [self createScrollView]; 
} 
+0

您需要擴展您的添加圖片。或者,另一種解決方案是在addImage末尾調用'createScrollView':' –

+0

您已經發布了'addImage:',這是您給我們的代碼。我假設你已經在你想添加圖片= P時調用它。我只是補充說'addImage:'還應該將圖像添加到視圖中,而不僅僅是數組。您應該始終確保支持它的視圖和數據結構是一致的。所以,如果你添加到數據結構,添加到視圖。如果從數據結構中刪除,請從視圖中刪除。 –

+0

已經嘗試過了,它會用舊的拇指重新添加。 – Bazinga

0

你需要調用createScrollView你更新了你的圖像後陣。也許嘗試將[self createScrollView];移至viewDidLoad方法的末尾?無論哪種方式,您需要在添加完所有圖像後調用createScrollView