3

我有问题UIscrollView,这就是我所做的:每当用户通过 a 选择相机胶卷中的图像(多个或单个)时Imagepicker,我想在我的 UIScrollView 中显示它。我能够显示它,但是当我Imagepicker再次选择一个图像时,它没有更新UIScrollView,我尝试将我[self createScrollView]的,viewDidAppear但它重新创建UIScrollView但不更新它,所以旧图像和新图像组合在一起. 所以我已经把它们放进去了,viewDidLoad但它只有在我去另一个View Controller然后再回来时才会更新。

// 我UIScrollView的缩略图

- (void) createScrollView {
    self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43, 370, 300, 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,134.0f)];
    [slotBg addSubview:self.scrollView];


    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, 60);
        [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(330, (row+1) * 60 + 10)];
}

// 在我的视图中DidLoad

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

编辑:

- (void) viewDidLoad {
    [self createScrollView];

    [_thumbs removeAllObjects];
    UIView *removeView;
    for(int i = 0; i < _thumbs.count; ++i) {
    while((removeView = [scrollView viewWithTag:i]) != nil) {
        [removeView removeFromSuperview];
    }

        { 
            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

1 回答 1

1

有两点:

  1. 检查您的数据源。图像是否已正确保存在您的文档目录中。
  2. 无需再次创建滚动视图及其父视图。将下面的代码从 移动createScrollViewviewDidLoad。这些视图应该只创建一次。

self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43, 370, 300, 143)]; CAGradientLayer *gradient = [CAGradientLayer 层]; 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,134.0f)];
[slotBg addSubview:self.scrollView];

现在,从 viewDidAppear,首先更新您的 _thumbs 数组(数据源)。然后调用 createScrollView 函数。在这个函数内部,首先从 UIScrollView 中移除所有使用标签的子视图。像你做的那样再次添加所有的拇指。然后在从 createScrollView 返回之前调用 [self setNeedsDisplay]。

于 2012-06-21T03:46:41.057 回答