Image Gallery

Discussion in 'iOS Programming' started by estupefactika, Mar 3, 2009.

  1. macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #1
    Hi, what is the best way to make a image gallery? I would like to display my little images and when I click in one of them to open a detail.

    a ViewController with UiScrollView?

    Any suggestion? Thanks
     
  2. thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #2
    How can I do the images clickables? with UIButton?
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    UIImageView is a subclass of UIResponder so you should be able to code it to react to touches, such as touchesBegan:withEvent: or touchesEnded:withEvent:
     
  4. thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #4
    Ok dejo, I'll look this methods, thanks.

    I have a doubt, Ive created several UIImageViews with their tag, if they have image I set it, easy, if it not I'll set later, my question, how can I set the image to my corresponding UIImageView through their tag?
     
  5. thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #5
    I do this and it works:

    UIImageView *img = (UIImageView *)[self.view viewWithTag:tag];

    [img setImage:loadImage];
     
  6. thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #6
    Hi again, Ive created a UIImageView to handle touches there.

    When I click an image, in the touchesBegan method, I would like to open a ViewController with their details.

    MyClass *viewController= [[MyClass alloc] initWithLink:storyLink];
    [[self navigationController] pushViewController:viewController animated:YES];

    but my UIImageView not respond to '-navigationController'

    What way do I have to use pushViewController correctly?
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Does your app have a UINavigationController in use?
     
  8. thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #8
    Yes, in my AppDelegate I have a TabBarController of UINavigationControllers. When I click in the corresponding tabBarItem, I open my image gallery viewController.
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    What is "self" in this line:
    Code:
    [[self navigationController] pushViewController:viewController animated:YES];
    and how is it defined? I.E. What does its .h look like?
    P.S. Put the .h within [ CODE ] [ /CODE ] tags to make it easier to read here.
     
  10. thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #10
    A summary:

    My AppDelegate has a UITabBarController of UINavigationControllers. When I click in corresponding tabBarItem display my imageGalleryViewController :

    imageGalleryViewController:

    Code:
    -(void)loadView {
    //Display my image gallery in a ScrollView
    //Each image belongs to a press release
    for (int i=0;i<[images count];i++) {
    ...
    MyImageViewClass *img=[[MyImageViewClass alloc] initwithFrame:CGRect(...) ];
    UIImage *image=[UIImage imageNamed:@"gray.png"];
    [img setImage:image];
    [scrollView addSubview:img];
    ....
    }
    
    }
    
    My gallery has been displayed.

    My UIImageViewClass:
    -Each image belongs to a press release, so I would like to open a ViewController with their press release detail.
    -I have to get the press release detail in an array. All images is ordered, Example, if I click in the first image, I searh the first index of the array and I get the press release detail.
    -Now, I should open my detailViewController to display the press release detail.
    Code:
    -(id) initWithFrame: (CGRect) aFrame {
    self=[super initWithFrame:aFrame];
    if (!self) return NULL;
    return self;
    }
    
    -(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    
    UITouch *touch=[[event allTouches] anyObject];
    int storyIndex=[touch view].tag;
    
    NSString *storyLink=[[appDelegate.feedArray objectAtIndex:storyIndex] objectForKey:@"url"];
    
    detailViewController *d=[[detailViewController alloc] initWithLink:storyLink];
    [[self navigationController] pushViewController:d animated:YES];
    }
    
    My detailViewController:
    Code:
    -(id) initWithLink:link {
    webView=[[UIWebView alloc] init];
    NSString *urlAddress=link;
    NSURL *url=[NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj=[NSURLRequest requestWithUrl:url];
    [self.webView loadRequest:requestObj];
    return self;
    }
    
    This is what I would like to do, but Ive read that ImageView has not NavigationController, by that dont works. I dont know if Ive explained good, so I dont know how i can to use the pushViewController
     
  11. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Are you sure about this? Because I really doubt that your UITabBarController has a set of UINavigationControllers, from the sound of things. Can you show us the code or Interface Builder inspector where you have set up the View Controllers for each of your tabs?
     
  12. thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #12
    Here is in my applicationDidFinishLaunching of AppDelegate:
    Code:
    for (int i = 0; i < [buttonNames count]; i++) {
    		switch (i) {
                        case 3:
                            viewController = [[imageGalleryViewController alloc] init];
                            break;
                        default:
    			viewController = [[otherViewController alloc] initWithCategory:i ];
                        break;
                    }
    			UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
    			UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:[buttonNames objectAtIndex:i] image:[UIImage imageNamed:[imgNames objectAtIndex:i]] tag:i];
    			nav.tabBarItem = tabBarItem;
    			
    			[controllers addObject:nav];
    			[viewController release];
    			[nav release];
    			[tabBarItem release];
    }
    
    // Create the toolbar and add the view controllers
    	tabBarController = [[UITabBarController alloc] init];	
    	[tabBarController setViewControllers:controllers animated:YES];
    	tabBarController.customizableViewControllers = controllers;
    	tabBarController.delegate = self;	 
    
    	// Set up the window
    	[window addSubview:tabBarController.view];
    
    It works every tabBarItem, but I dont know how I can to open the press release detail in other view when I click in an image.

    The functionality I want is like in photos app, instead of to open a image I open a viewController with a UIView with the press release detail and a 'Back button'

    But I unknown what method to use to do the transition to the next view
     

Share This Page