自定义图像选择器/ UIScrollView未出现在视图中

时间:2012-06-18 05:30:34

标签: iphone ios xcode

这里的新开发者,我正在使用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.
}

3 个答案:

答案 0 :(得分:3)

首先在scrollview中添加按钮,在for循环后的最后一个添加在self.view中的scrollview。

- (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];
}

我希望它适合你。

答案 1 :(得分: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.
}

答案 2 :(得分: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的回答做。

希望这会对你有所帮助。