UIScrollView错误地更新/加载

时间:2012-07-13 03:45:02

标签: iphone ios xcode

我在我的ViewController预览imagePicker中的所有已挑选图片都在我的scrollView中, 是的,我能够在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 {

    }
}    

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

1 个答案:

答案 0 :(得分:7)

以下是一个工作示例:Google Code

我写了代码,我有这个。它会将视图对齐为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)];
        }
    }
}