1

我正在创建一个程序,如果我单击 collectionview 中的缩略图,则应在另一个视图收集器中的滚动视图中打开更大的图像。为此,我正在使用 segue。但是我在那里做错了,我该如何解决这个问题,

我的segue代码是,

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

详细视图控制器.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];


}

那么我应该在滚动视图控制器(GalleryImageScrollViewController)中传递什么来打开特定缩略图的图像?

4

2 回答 2

1

像这样将图像名称传递给详细视图控制器

-(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
}

然后在detailViewController的viewDidLoad中

- (void)viewDidLoad
 {
    [super viewDidLoad];

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

我没有使用滚动视图,只有一个 imageView 作为子视图添加到视图中

在此处输入图像描述

于 2013-03-15T05:32:04.223 回答
0

它很简单。只需将所选缩略图的索引传递给 GalleryImageScrollViewController。(来自上面给出的答案..)

然后,在 detailViewController 的 viewDidLoad 方法中,使用获取的索引在 imageView 中填充图像。

detailViewController的 viewDidLoad 中

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

请注意,您的捆绑包中的文件扩展名是正确的。

于 2013-03-15T07:01:36.903 回答