Search code examples
iphoneiosxcodeicarousel

How to add images dynamically from camera/UIImagePickerController to iCarousel in iPhone


I am using iCarousel as shownh here .But I want to add images dynamically by taking a pic from camera or imagepicker. following is my code,

- (void)loadView {

    [super loadView];
    imagesArray = [[NSMutableArray alloc]init] ;
    // Initialize and configure the carousel
    carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0,50, 100, 100)];
    carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth |   UIViewAutoresizingFlexibleHeight;
    carousel.type = iCarouselTypeCoverFlow;
    carousel.dataSource = self;
    [self.view addSubview:carousel];
}
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [imagesArray count];

}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
    //note: placeholder views are only displayed on some carousels if wrapping is disabled
    return  [imagesArray count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
    UIImage *image = [imagesArray objectAtIndex:index];
    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0,50,100, 100)] autorelease];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    button.tag=index;
    return button;

}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [imagesArray addObject:image];
    NSLog(@"array count is %d",[imagesArray count]);
    [picker dismissModalViewControllerAnimated:YES];


}

Even I able to add the images to array , Only one image is appearing the imageView in coverflow. Can Any one please help me out.


Solution

  • You need to call reloadData on the carousel after modifying the array.