0

我在删除滚动视图中的图像时遇到问题,我将图像放在 UIScrollView 中,然后添加按钮,单击时会提示一个警报视图,询问您是否要删除它。NSDocumentDirectory如果是,则在我的视图中但不在我的视图中删除。我的图像是NSDocumentDirectory从一个ImagePicker.

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

4 回答 4

1

createScrollView每次将图像添加到数组时,您都会调用。它似乎在做的是创建包含相同图像的按钮。您的代码似乎正在将其从视图中删除,但它下方似乎有类似的按钮。因此,createScrollView只有在您添加了所有图像并将其从addImage:.

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];
于 2012-06-25T07:10:25.473 回答
0

试试这个方法:

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

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

    scrollView.contentSize = CGSizeZero;
    [self createScrollView];
}
于 2013-05-30T10:09:28.227 回答
0
//after deleting the image from document directory just call viewDidAppear again...

[self viewDidAppear:yes];

希望对你有帮助...

于 2012-06-25T07:30:05.923 回答
0

将标签值设置为按钮。然后通过以下函数获取按钮

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

于 2012-06-25T06:57:35.803 回答