2012-06-25 53 views
0

我無法在我的滾動視圖去除用圖像,我把我的圖片UIScrollView中再添加按鈕,點擊時會提示,詢問是否要刪除它的警報視圖。如果是,刪除我的NSDocumentDirectory,但不是在我的查看。我的照片是從ImagePicker挑選的NSDocumentDirectory按鈕刪除陣列,但不是在查看

- (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)]; 
} 

- (IBAction)buttonClicked:(id)sender { 
     _clickedButton = (UIButton *)sender; 
     UIAlertView *saveMessage = [[UIAlertView alloc] initWithTitle:@"" 
                   message:@"DELETE?" 
                  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"]) { 
      UIButton *button = _clickedButton; 
      [button removeFromSuperview]; 
      [_images objectAtIndex:button.tag]; 
      [_images removeObjectAtIndex:button.tag]; 

      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"); 
} 

- (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:@"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]); 
} 

回答

0

設置標籤值button.then由

(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
     UIButton *btn = (UIButton*)[scrollview viewWithTag:button.tag]; 
     //your button tag 
     [btn removeFromSuperView]; 
} 

+0

不行的,試過你說的 – Bazinga

+0

然後用NSLog的打印按鈕在您創建和刪除的兩個地方參考。 – Madhumitha

1

你每次圖像添加到您的陣列調用createScrollView得到下面的功能按鈕。它似乎在做的是創建包含相同圖像的按鈕。你的代碼似乎是從視圖中刪除它,但它看起來像它下面有類似的按鈕。所以只有在您添加了所有圖像並將其從addImage:中刪除後才致電createScrollView

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"); 
    } 
} 
[self createScrollView]; 
+0

當我添加其他圖像時,它不會更新滾動視圖中的圖像。 – Bazinga

+0

@ Kobe.o4:如果你的意思是你在viewDidAppear之外調用時,那麼你必須編寫代碼以確保它將它添加到滾動視圖中。否則,快速解決方法是在createScrollView的開頭調用[scrollview.subviews valueForKey:@「removeFromSuperview」]。顯然不是最好的方式,但會完成它。 – MadhavanRP

+0

我正在做的是刪除所有_thumbs,然後重新加載它。 – Bazinga

0
//after deleting the image from document directory just call viewDidAppear again... 

[self viewDidAppear:yes]; 

希望這將幫助你...

+0

已經做了這個,但它重新創建的視圖, – Bazinga

+0

是在調用它之前嘗試運行下面的代碼.. NSArray * arr = [scrollView subViews]; for(UIView * view in arr) [view removeFromSuperView]; 然後調用[self viewDidAppear:yes]; – Abhishek

0

試試這個方法:

- (void)removeObjectWithTag:(int)objectTag 
{ 
    [_thumbs removeObjectAtIndex:objectTag]; 

    for (UIView *view in scrollView.subviews) 
    { 
     [view removeFromSuperview]; 
    } 

    scrollView.contentSize = CGSizeZero; 
    [self createScrollView]; 
}