3

我在我的预览中ViewController所有我挑选的图像imagePicker都将在我的滚动视图中,是的,我能够在其中进行预览thumbnail,但是当我在调试器中记录它时,似乎每次我的viewDidAppear,它还会重新添加,scrollView因此图像数量会再次添加,由于图像重叠,因此更难在视图中删除。我需要的是scrollview在视图出现时和添加新图像时刷新。

这是我长期以来遇到问题的那些代码的预览:

- (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 {

    }
}    

删除:

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

1 回答 1

7

这是一个工作示例:谷歌代码

我写了代码,我有这个。它将视图对齐为 5 宽,无论它必须有多高,并且滚动视图将改变高度。

您将需要创建一个包含按钮列表的新NSMutableArray名称。_buttons

- (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)];
        }
    }
}
于 2012-07-16T15:53:08.323 回答