0

这里是新开发人员,我使用的是 ray wenderlich 的自定义图像选择器。但我想要做的不是通过按下按钮而是在视图加载时显示选择器。我似乎无法让它出现。这是我所做的,我不知道似乎有什么问题。谢谢你的帮助。

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

- (void) createScrollView {
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,200.0f)];

    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*140+24, row*150+10, 100, 100);
        [button setImage:thumb forState:UIControlStateNormal];
        [button addTarget:self 
                   action:@selector(buttonClicked:) 
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = i; 
        [self.view addSubview:view];
        [view addSubview:button];

        if (column == 6) {
            column = 0;
            row++;
        } else {
            column++;
        }

    }

    [view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
}
- (IBAction)buttonClicked:(id)sender {
    UIButton *button = (UIButton *)sender;
}

- (void)viewDidLoad
{
    [self createScrollView];
    _images =  [[NSMutableArray alloc] init];
    _thumbs =  [[NSMutableArray alloc] init];

    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]]; 
        NSLog(@"savedImagePath=%@",savedImagePath);
        if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
            [_images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 

            NSLog(@"file exists");
        } 
        NSLog(@"file does not exist");
    }
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
4

3 回答 3

3

第一次在滚动视图中添加按钮,最后在 for 循环之后在 self.view 中添加滚动视图。

- (void) createScrollView {
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,200.0f)];

    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*140+24, row*150+10, 100, 100);
        [button setImage:thumb forState:UIControlStateNormal];
        [button addTarget:self 
                   action:@selector(buttonClicked:) 
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = i; 

        [view addSubview:button];

        if (column == 6) {
            column = 0;
            row++;
        } else {
            column++;
        }

    }

    [view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];

  [self.view addSubview:view];
}

我希望它对你有用。

于 2012-06-18T05:45:42.047 回答
0

您首先创建滚动视图,然后初始化图像数组,因此它不会出现。

宁可使用 -

- (void)viewDidLoad
{
_images =  [[NSMutableArray alloc] init];
_thumbs =  [[NSMutableArray alloc] init];

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]]; 
    NSLog(@"savedImagePath=%@",savedImagePath);
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        [_images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 

        NSLog(@"file exists");
    } 
    NSLog(@"file does not exist");
}

[self createScrollView];

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
于 2012-06-18T05:34:49.233 回答
0

请注意注释行

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

[super viewDidLoad];在调用之后你应该做任何事情。

其次,在您的createScrollView方法中,您将滚动视图添加到self.view _thumbs.count时间(认为您只使用一个滚动视图,如果这样添加一次就足够了)。

所以把这段代码

 [self.view addSubview:view];

在 for 循环之上。

也按照rishi的回答做。

希望这对您有所帮助。

于 2012-06-18T05:44:51.553 回答