UIScrollView无法正确加载/更新

时间:2012-06-25 02:19:09

标签: iphone ios xcode

我的UISCrollView存在问题,UIScrollVIew位于我的View2Controller,我的UIScrollView包含图片缩略图,我从我的ImagePicker中导入图像(多个/单个图像)。我已将[self createScrollView];放入viewDidAppear,所以在我第一次加载视图时只显示UIScrollView,显然是因为我还没有选择图像。所以在我选择不在我的视图中更新之后。但我的images.count正在我的调试器中更新。它没有在View中更新,但是当我转到另一个ViewController然后再次返回View2Controller时,它会在我的UIScrollView中加载图片,然后当我再次添加图片时,它不会更新。为什么会这样?。

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

- (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)viewDidAppear:(BOOL)animated
{
    [self createScrollView];
}

- (void)viewDidLoad
{ 
     [super 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];
            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]);
}

编辑:(在此部分中,无法从视图中删除图片。但在我的NSDocumentDirectory中,图片已被删除,)

- (IBAction)buttonClicked:(id)sender {

        _clickedButton = (UIButton *)sender;
        UIAlertView *saveMessage = [[UIAlertView alloc] initWithTitle:@""
                                                              message:@"DELETE IMAGE?"
                                                             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"]) {
        NSLog(@"YES was selected.");
        UIButton *button = _clickedButton1;
        [button removeFromSuperview];
        [_images1 objectAtIndex:button.tag];
        [_images1 removeObjectAtIndex:button.tag];

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

2 个答案:

答案 0 :(得分:0)

只有在您进入视图后才会调用

viewDidAppear:。因此,当您“添加”图像时,只将它们添加到_images数组中,它们不会显示,因为您没有将它们添加到屏幕上。当您离开视图并返回时会显示它们,因为再次调用viewDidAppear:

要解决此问题,请使用addImage:将其添加到当前视图。

- (void)addImage:(UIImage *)image {
    int row = ?;
    int column = ?;
    [_images addObject:image];
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]];
    UIImage *thumb = [_thumbs objectAtIndex:_thumbs.count-1];
    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 = _thumbs.count-1; 
    [scrollView addSubview:button];
}

唯一的问题当然是如何确定位置,即列和行。您可以通过获取最后一个拇指的框架并根据屏幕边界确定放置位置来实现此目的。

另一个修复

- (void)addImage:(UIImage *)image {
    [_images addObject:image];
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]];
    [self createScrollView];
}

答案 1 :(得分:0)

更新完图片后,需要致电createScrollView。也许尝试将[self createScrollView];移到viewDidLoad方法的末尾?无论哪种方式,您都需要在添加完所有图片后致电createScrollView