How to add images dynamically from camera/UIImagePickerController to iCarousel in iPhone - 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.

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

Related

Change frame of cancel button in UISearchBar

I have the following code to change the appearance of the UISearchBar in my application and it can be seen in the image below also:
for(int i = 0; i < [[searchBar subviews] count]; i++){
UIView *subView = [[searchBar subviews] objectAtIndex:i];
if([[NSString stringWithFormat:#"%#", [subView class]] isEqualToString:#"UINavigationButton"]){
UIButton *cancelButton = (UIButton *)subView;
CGRect buttonFrame = cancelButton.frame;
buttonFrame.size.height = 52;
[cancelButton setFrame:buttonFrame];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel_pressed.png"] forState:UIControlStateHighlighted];
}
}
As you can see I am trying to change the height of the button contained in the UISearchBar with no luck. I do get reference to the button as I can change the text and the background Image is changed just not the height of the frame. I would just like the height of the button to be the same as the search box at 52px.
EDIT:
I have found a really hacky solution, but it's not very elegant of adding a UIButton as the subview of the Cancel Button. It does the job but as I say it's not very nice.
Actually you could use method below to access the cancel Button:
UIBarButtonItem *cancelButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
So I override -layoutSubViews with this, but theres 2 issues I have
I hate iterating through the views to find the matching view, and it messes up the animation a little bit
- (UIButton *)cancelButton {
for (UIView *v in self.subviews) {
if ([v isKindOfClass:[UIButton class]]) {
NSString *caption = [[((UIButton *)v) titleLabel] text];
if ([caption isEqualToString:#"Cancel"] )
return (UIButton *)v;
}
}
return nil;
}
- (BOOL)cancelButtonIsShowing {
return ([self cancelButton] != nil);
}
- (void)layoutSubviews {
[super layoutSubviews];
if ([self cancelButtonIsShowing]) {
UIButton *cancelButton = [self cancelButton];
UIImage *i = [UIImage imageNamed:#"Cancel_BTN"];
CGRect f = CGRectMake(267, 5, i.size.width, i.size.height);
cancelButton.frame = f;
}
}

UIButton ImageView Taking More Space in iCarousel and not Responding to touch Event

I am beginner trying to make a menu using iCarousel. What I have tried so far is return a UIButton with a custom image for each view in carousel. The image list has been declared same as told in examples. Problems I am facing are:
button (view returned in carousel) - its image is not adjusting properly as shown in fig.
Also you can see that when I am displaying bear, it is picking description of Tiger!
When I tap any pic, it just scroll forward instead of reacting to button touch down event.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//set up carousel data
wrap = YES;
self.menuItems = [NSMutableArray arrayWithObjects:#"Bear.png",
#"Zebra.png",
#"Tiger.png",
#"Goat.png",
#"Birds.png",
#"Giraffe.png",
nil];
self.descriptions = [NSMutableArray arrayWithObjects:#"Bears Eat: Honey",
#"Zebras Eat: Grass",
#"Tigers Eat: Meat",
#"Goats Eat: Weeds",
#"Birds Eat: Seeds",
#"Giraffes Eat: Trees",
nil];
}
return self;
}
- (void)buttonTapped:(UIButton *)sender
{
NSInteger index = [aCarousel indexOfItemViewOrSubview:sender];
switch (index) {
case 0:
NSLog(#"at index: %d",index);
break;
case 1:
NSLog(#"at index: %d",index);
break;
case 2:
NSLog(#"at index: %d",index);
break;
case 3:
NSLog(#"at index: %d",index);
break;
case 4:
NSLog(#"at index: %d",index);
break;
case 5:
NSLog(#"at index: %d",index);
break;
} }
- (void)viewDidLoad
{
aCarousel.type = iCarouselTypeInvertedRotary;
aCarousel.delegate = self;
aCarousel.dataSource = self;
//aCarousel.frame = CGRectMake(0, 68, 320, 257);
[super viewDidLoad];
}
#pragma - mark iCarousel Delegate
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [self.menuItems count];
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
// limit the number of item views loaded concurrently (for performance)
return 4;
}
- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, 240.0f, 200.0f);
[button setImage:[UIImage imageNamed:[menuItems objectAtIndex:index]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
//button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
return button;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
return 0;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
// usually this should be slightly wider than the item views
return 240; //for 220 px image
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
return self.wrap;
}
- (void)carouselDidScroll:(iCarousel *)carousel
{
[label setText:[NSString stringWithFormat:#"%#", [descriptions objectAtIndex:carousel.currentItemIndex]]];
}
I am really fed up trying adjusting carousel for hours. Although I tried aspectfitratio, adjusting frame but don't know why output is wrong. Can please someone figure out where am I doing wrong? Thanks alot :(
Atlast I was able to solve issue by myself. Don't know why my project was behaving so odd, so I decided to use iCarousel in new Project. The mistakes I noted were as follows:
In nib file, when I was making class type to iCarousel for subview, it was showing something like "I Carousel" in objects list - I changed the name in the list manually to my IBOutlet iCarousel object as "aCarousel".
Secondly I wasn't properly assigning UIButton to UIView in -(UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view method. I am glad that I found "Button demo" in Examples folder. From there I copied my code and now everything is working awesome! :)
Copying Code here for anybody who wants to see:
- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [UIImage imageNamed:[menuItems objectAtIndex:index]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
//[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
//button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
}
//set button label
//[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
//Use these commented lines if you are displaying some text on button
return button;
}

How to implement delete functionality for images in different positions in iCarousel iphone

I am using iCarousel as shownh here with carousel type Liner Carousel and implemented delete functionalty .I am able to delete the image in carousel and when I attempt to delete any other iomage in the visible screen It is moved to carousel frame and deleted.
I need to delete the image from its original position.
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor blackColor];
carousel = [[iCarousel alloc] initWithFrame:CGRectMake(-130,300, 320, 100)];
carousel.dataSource = self;
carousel.delegate=self;
carousel.type = iCarouselTypeLinear;
carousel.scrollEnabled=YES;
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(60, 50, 200, 200)];
[self.view addSubview:imageView];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImage *image = [imagesArray objectAtIndex:index];
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0, 60, 60);
[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;
NSLog(#"tag is %d",button.tag);
UIButton *deleteButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteButton.frame= CGRectMake(50, -5, 20 ,20);
UIImage *img = [UIImage imageNamed:#"close.png"];
[deleteButton setImage:img forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:deleteButton];
return button;
}
-(void)deleteButtonClicked:(int )sender
{
NSInteger currentIndex = carousel.currentItemIndex;
[carousel removeItemAtIndex:currentIndex animated:YES];
}
Please help me out.
You shouldn't delete the item at the carousel.currentItemIndex because that is not the item corresponding to the button you clicked, that's just the currently centred item in the carousel.
To get the correct item index for the button you clicked, do this:
-(void)deleteButtonClicked:(id)sender
{
//get the index of the item view containing the button that was clicked
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
//update the data model (always do this first)
[imagesArray removeObjectAtIndex:index];
//remove item from carousel
[carousel removeItemAtIndex:index animated:YES];
}
Try this:
NSInteger index = carousel.currentItemIndex;
[carousel removeItemAtIndex:index animated:YES];
[imagesArray removeObjectAtIndex:index];
add this also:
- (void)awakeFromNib
{
if (self) {
//set up carousel data
wrap = NO;
}
}
or make your
carousel.type = iCarouselTypeCustom;
to
carousel.type = iCarouselTypeLinear;
then implement this: [carousel reloadData];

adding reflection to iCarousel

I tried to add reflection to my icarousel. Firstly my code was like this and it was working wright.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton* button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
return button;
}
After changing, my code became like this:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (button == nil)
{
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
[view update];
return view;
}
Reflection appears. But the problem is when i move the carousel view, images not flowing correctly. Sometimes another carousel view appears.
Do you have any idea what causes that?
Code should be:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (view == nil)
{
//no view available to recycle, so create new one
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = view.bounds;
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 99;
[view addSubview:button];
}
else
{
button = (UIButton *)[view viewWithTag:99];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
[button setBackgroundImage:image forState:UIControlStateNormal];
[view update];
return view;
}
I use images with reflection built in them... don't need to do anything in code that way.. :p
I found my mistake. I am loading images here:
-(void) getImagesWithThread{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//for(int i=0;i<bookCount;i++){
for(int i=0;i<bookCount;i++){
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [arrBookImages objectAtIndex:i]]];
UIImage *image = [[UIImage imageWithData: imageData] retain];
image = [self scaleToSize:CGSizeMake(140, 200) withImage:image];
if(image!=nil) [arrBookImages replaceObjectAtIndex:i withObject:image];
[image release]; }
[carousel reloadData];
[pool drain];
}
When i performed this in background it didn't reload correctly. İf i perform normally, it works. But i want to load them in a thread. I mean first i want to set images and replace them images from url. but i want them to appear one by one. Do you have any idea?

UIButtons on tableFooterView not responding to events

I've successfully added some UIButtons to a custom tableFooterView. The problem is that the button events does not get called. Also, the image relative to the UIControlStateHighlighted doesn't come up. Any ideas?
I've tried all I could think of, but I can't find the trick. I'm reporting here the interesting part of the UITableViewController:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section
{
CGRect footerFrame = CGRectMake(0.0, 0.0, 480, 190);
_footerView = [[UIImageView alloc] initWithFrame: footerFrame];
_footerView.image = [UIImage imageNamed:#"tableviewfooter.png"];
_footerView.contentMode = UIViewContentModeTopLeft;
UIButton *addExam = [self createButtonWithFrame: CGRectMake(10, 30, 460, 58) Target: self Selector: #selector(addNewItem:) Image: #"additem.png" ImagePressed: #"additem_pressed.png"];
[_footerView addSubview:addExam];
return _footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger) section
{
return 190;
}
- (void) addNewItem:(id)sender
{
// This get's never called
NSLog(#"Pressed");
}
- (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
{
UIButton *button = [[UIButton alloc] initWithFrame:frame];
UIImage *newImage = [UIImage imageNamed: image];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
return button;
}
Thanks for your help!
I do not think UIImageView was designed to work well as a container for other views, especially UIButtons. For one, UIImageViews, when created, have their userInteractionsEnabled property set to false by default.
I would just create a simple UIView as your footer view and then add both the UIButton and the UIImageView directly to it.