0

我想在单击缩略图时在滚动视图中的特定图像上显示图像。根据缩略图的选择,我想显示图像。我只是在使用 ScrollView,那我该如何解决呢?我的collectionview代码是,

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";
    customcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [[cell nyimage]setImage:[UIImage imageNamed:[temparr objectAtIndex:indexPath.section * 1 + indexPath.row]]];
    return cell;

}


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

        NSString *imageNameToLoad = [NSString stringWithFormat:@"%d", selectedIndexPath.row];
        NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"jpg"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
        GalleryImageScrollViewController *gallerydetailViewController = [segue destinationViewController];
        gallerydetailViewController.FullScreenImageScroller=image;
    }
}

滚动视图是,

- (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];


}
4

1 回答 1

0

如果我正确理解您的代码,如果您根据缩略图的索引更改滚动视图的偏移量,它应该可以工作:

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

                NSString *imageNameToLoad = [NSString stringWithFormat:@"%d", selectedIndexPath.row];
                NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"jpg"];
                UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
                GalleryImageScrollViewController *gallerydetailViewController = [segue destinationViewController];

                //set offset of scroll view
                gallerydetailViewController.FullScreenImageScroller setContentOffset:CGPointMake(WIDTH_OF_IMAGE*selectedIndexPath.row, 0);
            }
        }
于 2013-03-15T11:50:41.100 回答