1

I am creating a program where if I click on thumbnail in collectionview, larger image should be opened in scroll view which is in another view collector. For that purpose i am using segue. but I am doing something wrong there, how can I solve this problem,

my code of segue is,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"])
    {
        NSIndexPath *selectedIndexPath = [[self.gallerycollection indexPathsForSelectedItems] objectAtIndex:0];

        // load the image, to prevent it from being cached we use 'initWithContentsOfFile'
        NSString *imageNameToLoad = [NSString stringWithFormat:@"interior_%d", selectedIndexPath.row];
        NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"jpg"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];

        GalleryImageScrollViewController *gallerydetailViewController = [segue destinationViewController];
        gallerydetailViewController.FullScreenImageScroller=image ;
    }
}

detailViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSMutableArray *mutablearray = [NSMutableArray array];
    data=[MyDatabase new];
    slideImages=[data OpenMyDatabase:@"SELECT pic_name_big FROM interior":@"pic_name_big"];
    [mutablearray addObjectsFromArray:slideImages];
    temparr = [[NSMutableArray alloc]init];
    temparr=[NSMutableArray arrayWithArray:mutablearray];
    [self putImageViewsInScrollView:[temparr count]];
    self.FullScreenImageScroller.delegate=self;

}




-(void) putImageViewsInScrollView:(int)numberOfImageViews
{
   for(int i=0 ;i<numberOfImageViews; i++)
    {
        fullScreenImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[temparr objectAtIndex:i]]];
        fullScreenImageView.frame = CGRectMake((WIDTH_OF_IMAGE * i)  , 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
        [self.FullScreenImageScroller addSubview:fullScreenImageView];
    }
    [self.FullScreenImageScroller setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([temparr count]), HEIGHT_OF_IMAGE)];
    [self.FullScreenImageScroller setContentOffset:CGPointMake(0, 0)];
    [self.FullScreenImageScroller scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];


}

then what should I pass in scrollview controller (GalleryImageScrollViewController) to open image of specific thumbnail ?

user2147631
  • 83
  • 1
  • 2
  • 9

2 Answers2

1

Pass the image name to the detail view controller like this

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
   NSArray *indexpaths = [self.collectionView indexPathsForSelectedItems];
   DetailViewController *dest = [segue destinationViewController];
   dest.imageName = [recipeImages objectAtIndex:[[indexpaths objectAtIndex:0] row]];
   // imageName is a property of detail view controller
}

Then in the viewDidLoad of detailViewController

- (void)viewDidLoad
 {
    [super viewDidLoad];

   self.imageView.image = [UIImage imageNamed:self.imageName];
}

Im not using scroll view, only an imageView added as a sub view to the view

enter image description here

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • thanks mate. I got it. but how to pass that to scrollview in another view controller. – user2147631 Mar 15 '13 at 05:36
  • You can use the same logic. Add one imageView to scrollView then set the image. scrollView or not doesnt matter you can set image only on a imageView. So add imageView to scrollView – Anil Varghese Mar 15 '13 at 05:41
  • @ Anil: what i did is, I have added a imageview to scrollview to display images. so m not getting what to do in viewDidLoad of detailViewScroller – user2147631 Mar 15 '13 at 05:51
  • Ok will help you. create an outlet for the imageView in detail view controller then set image in viewDidLoad as I did. I hop you already connected your cell to detailViewController through segue. For displaying image this is enough. further you can adjust content size of scroll view – Anil Varghese Mar 15 '13 at 06:05
  • thanks mate :) dont this with UIScrollView. what should I include in detailViewController ? – user2147631 Mar 15 '13 at 06:46
  • want to know you got anything in detailView? – Anil Varghese Mar 15 '13 at 06:49
  • yes got images in detailview but with normal index. I want the image of thumbnail that i've clicked. – user2147631 Mar 15 '13 at 08:27
  • normal index? i didnt get you. Actually when you click on the cell the detail view appears right? i prepareForSegue setting `dest.imageName = [recipeImages objectAtIndex:[[indexpaths objectAtIndex:0] row]];` this is the name of the selected image(if you want you can change into index value) – Anil Varghese Mar 15 '13 at 08:42
0

Its pretty simple. Just pass the index of selected thumbnail image to GalleryImageScrollViewController.(from answers given above..)

Then, In the viewDidLoad method of detailViewController populate image in imageView using the fetched index.

In the viewDidLoad of detailViewController

 -(void)viewDidLoad
  {
       [super viewDidLoad];
       self.imageView.image = [UIImage imageNamed:[[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"interior_%d", indexOfThatImage] ofType:@"png"]]];
  }

Please note that file extension is correct in that of your bundle.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Akhil PK
  • 91
  • 5