3

apple beginner here. Im currently studying and trying the iCarousel by nickLockwood. Im planning on putting images in the iCarousel. So here's my .m and .h code:

// .m

@implementation ViewController

@synthesize images;
...

- (void)awakeFromNib
{    
    if (self) {

        //set up carousel data
        //wrap YES = infinite loop
        wrap = YES;

        self.images = [NSMutableArray arrayWithObjects:
                       @"apple.jpg",@"lemon.jpg",@"orange.jpg",@"melon.jpg",@"mango.jpg",nil];       
            }

}


- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{   
    if (view == nil)
{

        view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[images objectAtIndex:index]]];
}
    return view;

}

// .h

@interface ViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>{

NSMutableArray *images;
...
}
@property (nonatomic,retain) NSMutableArray *images;
…
@end

I was planning on adding 30 images so I want the code to be maintainable. My problem is I want to be able to get the image by NSBundle or from the applications directory, I've tried different codes but the image doesn't appear in the carousel. Thanks for your help.

Bazinga
  • 2,456
  • 33
  • 76

1 Answers1

1

Hey here is some code of mine that i used in my iCarousel app. You are missing some methods. So here they are.

#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [items count];
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    //limit the number of items views loaded concurrently (for performance reasons)
    //this also affects the appearance of circular-type carousels
    return 7;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[items objectAtIndex:index]]];
    return view;

}

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

- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[items objectAtIndex:index]]];
    return view;

}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    //usually this should be slightly wider than the item views
    return ITEM_SPACING;
}

- (CGFloat)carousel:(iCarousel *)carousel itemAlphaForOffset:(CGFloat)offset
{
    //set opacity based on distance from camera
    return 1.0f - fminf(fmaxf(offset, 0.0f), 1.0f);
}

- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carousel.itemWidth);
}

- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return wrap;
}
- (void)carouselDidEndScrollingAnimation:(iCarousel *)aCarousel
{    
    [labelDescription setText:[NSString stringWithFormat:@"%@", [descriptions objectAtIndex:aCarousel.currentItemIndex]]];
}

Hope this helps.

Mou
  • 31
  • 5
  • How about from the main bundle? – Bazinga May 24 '12 at 04:01
  • O ya and add carousel.type = iCarouselTypeCoverFlow2; in your viewDidLoad – Mou May 24 '12 at 08:25
  • its already done. my problem is uploadingg images from NSBundle or from camera roll. thanks – Bazinga May 24 '12 at 08:30
  • 2
    You don't need most of the methods, they are for implementing the custom transform in the example app. The only methods that are required are numberOfItemsInCarousel: and carousel:viewForItemAtIndex:reusingView:, everything else is optional. – Nick Lockwood Jun 08 '12 at 15:51