Fetching Selected Image From An Array to be displayed in another view - ios

I'm trying to fetch the selected image from the array of images.And the selected image should be displayed in new view..
- (void)viewDidLoad{
[super viewDidLoad];
selectedTag = 0;
imagesName = [[NSArray alloc]initWithObjects:#"nature1.jpg",#"nature2.jpg",#"nature3.jpg",#"nature4.jpg",#"nature5.png",#"nature6.jpg",nil];
images = [[NSMutableArray alloc]init];
imageScroll.delegate = self;
imageScroll.scrollEnabled = YES;
int scrollWidth = 768;
imageScroll.contentSize = CGSizeMake(scrollWidth,605);
int xOffset = 0;
for(index=0; index < [imagesName count]; index++)
{
UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
img.tag = index;
selectedTag = img.tag;
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(50+xOffset, 120, 270, 350);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
[img setImage:[UIImage imageNamed:[imagesName objectAtIndex:index]] forState:UIControlStateNormal];
[images insertObject:img atIndex:index];
imageScroll.contentSize = CGSizeMake(scrollWidth+xOffset,510);
[imageScroll addSubview:[images objectAtIndex:index]];
xOffset += 370;
[img addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchUpInside];
selectedTag = img.tag;
}
}
-(void)newView:(id)sender{
NSLog(#"%#",sender);
int f;
f = [sender tag];
CGRect rec = CGRectMake(0, 0, 250, 250);
[self setView:[[[UIView alloc] initWithFrame:rec] autorelease]];
[[self view] setBackgroundColor:[UIColor grayColor]];
UIImageView *im = [[UIImageView alloc] initWithImage:[imagesName objectAtIndex:f]];
im.bounds = CGRectMake(10, 10, 50, 50);
im.frame = CGRectMake(250, 120, 270, 350);
[[self view] addSubview:im];
}
Can anybody help me to fetch the selected image to another view?? Thanks In Advance..

You are setting string instead of image. Use this piece of code
UIImage *img = [UIImage imageNamed:[imagesName objectAtIndex:f]];
UIImageView *im = [[UIImageView alloc] initWithImage:img];

Related

How to open and close tableView on a button click?

2I'm working on dropDowns, i'm creating buttons and dropdown tableView dynamically. When i click first button last tableView was opened. But i want to open specific tableView (at a time i want to open only one dropdown).
float y = 0;
float x = (self.answersView.frame.size.width/2)+175;
float Iy = 0;
float Ix = 675;
float Tx = (self.answersView.frame.size.width/2)+175;
float Ty = 0;
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
[self.dropdownBtn setTitle:#"0" forState:UIControlStateNormal];
self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];
[self.dropdownBtn addTarget:self action:#selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview:self.dropdownBtn];
self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue];
y = y+70;
self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
self.dropdownImageView.image = [UIImage imageNamed:#"dropdown.png"];
[self.answersView addSubview:self.dropdownImageView];
Iy = Iy+70;
self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
self.content = #[ #"Monday", #"Tuesday", #"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday"];
self.dropdownTableView.delegate = self;
self.dropdownTableView.dataSource = self;
[self.answersView addSubview:self.dropdownTableView];
Ty = Ty+70;
self.dropdownTableView.hidden = YES;
}
//The event handling method
- (void)dropdownBtnTapMethod:(UIButton *) sender {
if (self.dropdownTableView.hidden == YES) {
self.dropdownTableView.hidden = NO;
} else {
self.dropdownTableView.hidden = YES;
}
}
And how to set table view height dynamically.
Try this :
NSMutableArray *arrTableview; // make global
In ViewDidLoad
arrTableview = [NSMutableArray New];
Change in following method
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.dropdownBtn.tag = i; // add this line
self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
[self.dropdownBtn setTitle:#"0" forState:UIControlStateNormal];
self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];
[self.dropdownBtn addTarget:self action:#selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview:self.dropdownBtn];
//self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue]; // This line commented.
y = y+70;
self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
self.dropdownImageView.image = [UIImage imageNamed:#"dropdown.png"];
[self.answersView addSubview:self.dropdownImageView];
Iy = Iy+70;
self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
self.content = #[ #"Monday", #"Tuesday", #"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday"];
self.dropdownTableView.delegate = self;
self.dropdownTableView.dataSource = self;
[self.answersView addSubview:self.dropdownTableView];
[arrTableview addObject :self.dropdownTableView];
Ty = Ty+70;
self.dropdownTableView.hidden = YES;
}
Selector
- (void)dropdownBtnTapMethod:(UIButton *) sender {
UITableView *tableView =[arrTableview objectAtIndex:sender.tag]
if (tableView.hidden == YES) {
tableView.hidden = NO;
} else {
tableView.hidden = YES;
}
}

How to start the photo scroller with the selected image from collectionView in ios

I have used the code
imageSlider = [[UIScrollView alloc]initWithFrame:CGRectMake(5, 60, 700, 700)];
imageSlider.pagingEnabled = YES;
imageSlider.showsHorizontalScrollIndicator =NO;
for (int i = 0; i < [myObject count]; i++) {
CGRect frame;
frame.origin.x = imageSlider.frame.size.width * i;
frame.origin.y = 0;
frame.size = imageSlider.frame.size;
NSManagedObject *con = [myObject objectAtIndex:i] ;
UIImage *Image = [UIImage imageWithData:[con valueForKey:#"image"]];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 700, 700)];
imgView.image = Image;
imgView.frame = frame;
[imageSlider addSubview:imgView];
}
imageSlider.contentSize = CGSizeMake(imageSlider.frame.size.width*([myObject count]), 600);
imageSlider.delegate = self;
[self.view addSubview:imageSlider];
It starts from first image I dono how to start image from the selected image in previous viewController. Thanks in advance..

Image in full screen from scroll view

I have a UIScrollView and have images in that ScrollView and they are added programmatically. When I click on image, I want that image to be displayed on full screen.
What should I add to my code?
The number of images depend upon the [images count];
self.scrollView.contentSize = CGSizeMake(320, 134);
self.scrollView.delegate = self;
int X=0;
for (int i = 0; i < [images count]; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 140, 136)] ;
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
[imageView setImage: [images objectAtIndex:i]];
[self.scrollView addSubview: imageView];
X = X + imageView.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 134);
}
You should not add UIImageViews then.
Instead add UIButton of type Custom and add the Image to the Button and add an IBAction to the UIButtons which adds a FullScreenView with your image.
In your for() for each button add a tag with i and in your IBAction function get the tag with [sender tag] and get your image with [images objectAtIndex:[sender tag]]
Example:
-(void)test{
for (int i = 0; i < [images count]; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(X, 0, 140, 136);
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(showFullscreen:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
X = X + button.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 134);
}
}
-(IBAction)showFullscreen:(id)sender{
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)] ;
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [images objectAtIndex:[sender tag]]];
[self.view addSubview: imageView];
}

Showing UIButton Done on UIScrollview for images

Want to show UIButton done on UIScrollView for images. With the below code UIButton is showing but when I scroll UIImageViews, it shows only on very first imageview where as I want UIButton done should show on all image views in UIScrollView.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(0, 0, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
How should I code it that UIButton Done should show on all image views when I scroll it.
Add your UIButton in for loop i.e. it can add each time when your new ImageView is add.
And change its x_Origin for each imageView -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 0, 60, 35);
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
}
Remove these 2 lines
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
There is no need to do this. This means you're setting that button on rightBarButton of NavigationBar but in your case i think you dont want to do so.
Set frame for myButton in for loop similar to imageview frame.
myButton.frame= CGRectMake(xOrigin, 0, self.myButton.frame.size.width, self.myButton.frame.size.height);

uibutton is not getting displayed

I am trying to add uibutton inside the imageview.
But it was not displayed the button.
Even i tried adding it to the uiscrollview and also for self.view.
But nothing were displayed the uibutton
Pls let me know what is the problem
const CGFloat HEIGHT = 1024.0;
const CGFloat WIDTH = 768.0;
#define myViewPortrait CGRectMake(0.0f, 0.0f, 768.0f,1024.0f)
#define myViewLandSacpe CGRectMake(0.0f, 0.0f, 1024.0f,768.0f)
#define kAnimationKey #"animationKey"
-(void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [myScrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (self.view.frame.size.width);
}
}
[myScrollView setContentSize:CGSizeMake((myImagesCount * self.view.frame.size.width), [myScrollView bounds].size.height)];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myScrollView = [[UIScrollView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myScrollView];
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 1;
[rightRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftRecognizer.numberOfTouchesRequired = 1;
[leftRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
[myScrollView setBackgroundColor:[UIColor blackColor]];
[myScrollView setCanCancelContentTouches:NO];
myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollView.clipsToBounds = YES;
myScrollView.scrollEnabled = YES;
myScrollView.pagingEnabled = YES;
myScrollView.delegate = self;
myImagesCount = 5;
myScrollView.showsHorizontalScrollIndicator=NO;
myScrollView.showsVerticalScrollIndicator=NO;
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[myScrollView addSubview:imageView];
[imageView release];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
[self layoutScrollImages];
[super viewDidLoad];
}
Based off your question, you'd like to add the UIButton as a subview to the UIImageView - I'm guessing in the for loop? Immediate problem I see is that the actual button is being generated outside of the for loop. I think your for loop is intended to look like this:
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[imageView addSubview:button];
[myScrollView addSubview:imageView];
[imageView release];
}
Try this
Declare button globally and set
[self layoutScrollImages];
[self.view bringSubViewToFront:button];

Resources