2012-07-13 61 views
3

林進行預覽我的ViewControllerimagePicker所有我拿起圖像會在我的滾動視圖, 是的,我能夠做它的一個預覽在thumbnail,但是當我登錄它在我的調試器中,似乎每次我的viewDidAppear,它也reAddsscrollView,所以圖像計數再次被添加,使得由於圖像重疊在視圖中更難以刪除。我需要的只是刷新scrollview,無論何時出現視圖以及何時添加新圖像。的UIScrollView更新/加載錯誤

這是我在很長一段時間的問題,這些代碼先睹爲快:

- (id) initWithCoder:(NSCoder *)aDecoder { 
    if ((self = [super initWithCoder:aDecoder])) { 
     _images = [[NSMutableArray alloc] init]; 
     _thumbs = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 
- (void)addImage:(UIImage *)image { 
    [_images addObject:image]; 
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]]; 
    [self createScrollView]; 
}   
- (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)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]; 
} 


- (void)viewDidAppear:(BOOL)animated 
{  
    [_thumbs removeAllObjects]; 
    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]]; 
     if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
      [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 
     } 
    } 
} 

幫助將非常感激。

而且會使用這很多的幫助,刪除然後添加。或者這是一種完全刪除/刪除所有這些子視圖的方法,然後REadd? 感謝那些有所幫助的人。

這可能有幫助嗎?三江源

for(UIView *subview in [scrollView subviews]) { 
    if([subview isKindOfClass:[UIView class]]) { 
     [subview removeFromSuperview]; 
    } else { 

    } 
}  

DELETE:

- (void)deleteItem:(id)sender { 
     _clickedButton = (UIButton *)sender; 
     UIAlertView *saveMessage = [[UIAlertView alloc] initWithTitle:@"" 
                   message:@"DELETE?" 
                  delegate:self 
                cancelButtonTitle:@"NO" 
                otherButtonTitles:@"YES", nil]; 
} 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
if([title isEqualToString:@"YES"]) { 
     NSLog(@"YES was selected."); 
      UIButton *button = _clickedButton; 
      [button removeFromSuperview]; 
      [_images objectAtIndex:button.tag]; 
      [_images removeObjectAtIndex:button.tag]; 
      [_images removeObject:button]; 

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

嘗試刷新後添加新對象之前刪除從陣列中的所有對象,它可能工作。 – Dhruv 2012-07-13 04:22:58

回答

7

這裏是一個工作示例:Google Code

我寫出來的代碼,我有這個。它將對齊視圖爲5寬,並且無論高度如何,滾動視圖將改變高度。

您將需要創建一個名爲_buttons的新的NSMutableArray,其中將包含您的按鈕列表。

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

    int row = floor(([views count] - 1)/5); 
    int column = (([views count] - 1) - (row * 5)); 

    UIImage *thumb = [_thumbs objectAtIndex:[_thumbs count]-1]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); 
    [button setImage:thumb forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside]; 
    button.tag = [views count] - 1; 
    // This is the title of where they were created, so we can see them move.s 
    [button setTitle:[NSString stringWithFormat:@"%d, %d", row, column] forState:UIControlStateNormal]; 

    [_buttons addObject:button]; 
    [scrollView addSubview:button]; 
     // This will add 10px padding on the bottom as well as the top and left. 
    [scrollView setContentSize:CGSizeMake(300, row*60+20+60)]; 
} 

- (void)deleteItem:(id)sender { 
    UIButton *button = (UIButton *)sender; 
    [button removeFromSuperview]; 
    [views removeObjectAtIndex:button.tag]; 
    [_buttons removeObjectAtIndex:button.tag]; 

    [self rearrangeButtons:button.tag]; 
} 

- (void)rearrangeButtons:(int)fromTag { 
    for (UIButton *button in _buttons) { 
     // Shift the tags down one 
     if (button.tag > fromTag) { 
      button.tag -= 1; 
     } 
     // Recalculate Position 
     int row = floor(button.tag/5); 
     int column = (button.tag - (row * 5)); 
     // Move 
     button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); 
     if (button.tag == [_buttons count] - 1) { 
      [scrollView setContentSize:CGSizeMake(300, row*60+20+60)]; 
     } 
    } 
} 

注意:在rearrangeButtons方法,能夠進行動畫的變化。

這裏是重新安排文件的代碼:

- (void)rearrangeButtons:(int)fromTag { 
    for (UIButton *button in _buttons) { 
     // Shift the tags down one 
     if (button.tag > fromTag) { 
      // Create name string 
      NSString *imageName = [NSString stringWithFormat:@"images%i.png", button.tag]; 
      // Load image 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentFile = [paths objectAtIndex:0]; 
      NSSting *oldFilePath = [documentFile stringByAppendingPathComponent:imageName]; 
      NSData *data = [[NSData alloc] initWithContentsOfFile:oldFilePath]; 
      button.tag -= 1; 
      // Save the image with the new tag/name 
      NSString *newImageName = [NSString stringWithFormat:@"images%i.png", button.tag]; 
      NSString *newFilePath = [documentFile stringByAppendingPathComponent:newImageName]; 
      [data writeToFile:newFilePath atomically:YES]; 
      // Delete the old one 
      NSFileManager *fileManager = [NSFileManager defaultManager]; 
      NSError *err = nil; 
      if (![fileManager removeItemAtPath:file error:&err]) { 
       // Error deleting file 
      } 
     } 
     // Recalculate Position 
     int row = floor(button.tag/5); 
     int column = (button.tag - (row * 5)); 
     // Move 
     button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); 
     if (button.tag == [_buttons count] - 1) { 
      [scrollView setContentSize:CGSizeMake(300, row*60+20+60)]; 
     } 
    } 
} 
+0

那麼生病去除createScrollView部分? – Bazinga 2012-07-16 22:17:08

+0

我有這個部分的問題'int row = floor([_ thumbs count]/4); int column =([_thumbs count] - row);'由於縮略圖未正確對齊。 – Bazinga 2012-07-17 01:18:48

+0

也當我刪除它,它刪除了視圖,但沒有在'NSDocumentDirectory' – Bazinga 2012-07-17 01:26:07