【问题标题】:iOS set image of specific indexiOS设置特定索引的图像
【发布时间】:2013-03-04 02:01:43
【问题描述】:

我想在单击缩略图时在滚动视图中的特定图像上显示图像。根据缩略图的选择,我想显示图像。我只是在使用 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];


}

【问题讨论】:

  • 只要根据你要显示的索引修改contentOffset值即可。有什么问题?
  • 感谢您的回复伙伴。我想在单击缩略图时打开全屏图像。更大的图像应该在我已经编码的滚动视图中打开。
  • 你在哪里点击缩略图?这些缩略图在哪里?他们在另一个视图中吗?
  • 那些缩略图在收藏视图中。我已经编辑了上面的代码。请参考一次。
  • 如果我理解正确,这个“customcell”对象代表您的缩略图?您如何处理对它们的点击/选择?请您也添加此代码。

标签: ios uiscrollview indexing thumbnails


【解决方案1】:

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

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

【讨论】:

  • 为什么不发生什么?
  • 即使我在 sollcetion 视图中单击任何图像。它只是继续在滚动视图中显示第一张图片
  • 您能否添加您的完整代码,最好将整个文件添加到某个链接中,很难看到发生了什么。我刚刚试着给你指路。
  • 给我你的邮件ID。会邮寄给你。如果可能,请在同一个文件中进行更改并将其发回:) 谢谢
  • 我无法运行它,因此无法真正看到发生了什么。可能您在某处弄乱了索引。只是尝试放置一些断点。确保当您单击缩略图时,您会得到正确的索引。然后确保使用该索引来设置 scrollView 的 contentOffset。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2018-10-24
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多