4

在我的项目中,我使用的是UICollectionView来显示图像,我也在中使用UIImageView。 我想要的只是当我点击一张图片时,它应该展开并全屏显示。 为此,我创建了一个新视图,其上需要UIImageView并在UICollectionViewCells上应用TapGesture。 展开的图像在下一个视图中打开,但没有出现在我给它的框架中。 也当我点击任何图片时,它从集合视图中删除。请告诉我如何重新加载UICollectionViewCellsenter image description here 请建议我如何将图像放在规定的框架上。 我分享我的代码如何在UICollectionView中点击时将图像展开为完整视图?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 

    imgview = (UIImageView *)[cell viewWithTag:100]; 
    imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,160, 160)]; 

    imgview.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]]; 
    //cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]]]; 
    [cell.contentView addSubview:imgview]; 

    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandImage:)]; 
    tapped.numberOfTapsRequired = 1; 
    [imgview setUserInteractionEnabled:YES]; 
    imgview.tag = indexPath.row; 
    [imgview addGestureRecognizer:tapped]; 

    [cell.contentView addSubview:imgview]; 

    return cell; 
} 

-(void)expandImage:(UITapGestureRecognizer*)recogniser 
{ 
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width , self.view.frame.size.height)]; 
    view1.backgroundColor = [UIColor greenColor]; 
    [self.view addSubview:view1]; 

    UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(310, 10, 50, 40)]; 
    [closeButton setTitle:@"Close" forState:UIControlStateNormal]; 
    [closeButton addTarget:self action:@selector(Closetab) forControlEvents:UIControlEventTouchUpInside]; 
    [view1 addSubview:closeButton]; 

    UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height-200)]; 
    [photoView setBackgroundColor:[UIColor blueColor]]; 
    [view1 addSubview:photoView]; 
    photoView.accessibilityIdentifier = @"nature2.png"; 

    UIImageView *photoView1 = (UIImageView*)recogniser.view; 
    photoView1.accessibilityIdentifier = @"nature2.png"; 

    //photoView.clipsToBounds = YES; 

    // photoView.accessibilityIdentifier = @"apple1.png"; 

    photoView1.contentMode = UIViewContentModeScaleAspectFill; 

    //photoView1.clipsToBounds = YES; 
    [view1 addSubview:photoView1]; 
} 
+0

添加一些快照 –

+0

我不有足够的声望(15)分数添加快照把我的问题投票了,然后我会添加它的快照 –

+0

imageView是否出现?帧是否错误? – Aris

回答

1

因此,最好在窗口中添加视图,以便全屏显示。使用以下代码。

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width , self.view.frame.size.height)]; 
    view1.backgroundColor = [UIColor greenColor]; 
    [self.view.window addSubview:view1]; 
+0

如果你可以添加快速版本 – marrioa

1

一种更简单的方式来实现,这将是推动,而不是将其添加为一个子视图为您显示全屏画面有新的观点。

您还可以自定义导航控制器的过渡动画以适应您的需求。

1

好吧,不要把任何事情视为理所当然的事情,因为有一件事情是肯定的,那就是看着代码让我感到困惑。但这里是我怎么看它,哪里出了问题:

UIImageView *photoView1 = (UIImageView*)recogniser.view; 
    [view1 addSubview:photoView1]; 

与那些2线正在从电池获取了你的观点,并把它放在这个新厂景。 所以只要分配新的UIImageVIew并设置其图像可以解决它。 还有一些建议:

imgview = (UIImageView *)[cell viewWithTag:100]; 
    imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,160, 160)]; 

这里第一行代码没有做任何事。您设置对象,然后再次设置该对象。 接下来的事情是不要在cellForItemAtIndexPath添加子视图,而不删除以前添加。当将细胞排出时,您一次又一次地使用相同的细胞,因此您将通过将视图彼此堆叠来创建内存泄漏。你甚至可以通过用自己的UIImageView创建自定义单元来省略所有添加的东西。接下来就没有必要使用手势识别的CollectionView有这样的一个代表:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 

你可以用它代替您expandImage方法,里面你只是简单地从imagearray让你的形象。 最后还有这个美丽的图书馆可供您使用,MHFacebookImageViewer

1

navigationController.view

UIImageView *imgView =[[UIImageView alloc]initWithFrame:self.view.bounds]; 
[self.navigationController.view addSubview:imgView]; 
0

我这段代码 首先加点触手势 完成上述添加imageview然后采用下列代码

-(void)expandImage:(UITapGestureRecognizer*)recogniser 
{ 

    view1.hidden = NO; 

    UIImageView *photoView1 = (UIImageView*)recogniser.view; 
    photoView1.frame = CGRectMake(0, 0, self.view.frame.size.width, 510); 
    photoView1.accessibilityIdentifier = @"nature2.png"; 

    //photoView.clipsToBounds = YES; 

    // photoView.accessibilityIdentifier = @"apple1.png"; 

    photoView1.contentMode = UIViewContentModeScaleAspectFill; 

    photoView1.clipsToBounds = YES; 

    [view1 addSubview:photoView1]; 
    } 
相关问题