Bazinga
Bazinga

Reputation: 2466

Custom Image Picker/ UIScrollView not appearing in view

New developer here,Im using the Custom Image Picker by ray wenderlich. But what I wanted to do is show the picker not by pressing a button but when the view loads. I can't seem to make it appear. Here is what I have done, I dont know what seems to be wrong. Thanks for your help.

- (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.
}

Upvotes: 0

Views: 416

Answers (3)

Dhaval Bhadania
Dhaval Bhadania

Reputation: 3089

1st add the button in scrollview and at the last after for loop add the scrolview in 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];
}

i hope it work for you.

Upvotes: 3

Ananth
Ananth

Reputation: 845

Please notice the commented lines

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

You should do anything after the [super viewDidLoad]; is called.

Secondly, in your createScrollView method, you are adding the scrollview to self.view _thumbs.count times(think you are using only one scrollview, if so adding one time is enough).

So put the piece of code

 [self.view addSubview:view];

above the for loop.

Also do as per rishi's answer.

Hope this helps you.

Upvotes: 0

rishi
rishi

Reputation: 11839

You are creating scroll view first and then initialising image array, due to which it won't appear.

Rather Use like -

- (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.
}

Upvotes: 0

Related Questions