How to enlarge image to full view when used in UICollectionView?

In my project, I use UICollectionViewto display images, and I also use UIImageViewin UICollectionViewCell. All I want is when I click on the image that it should expand and show in full screen. To do this, I created a new view on which UIImageViewTapGesture is on and will apply UICollectionViewCells. The expanded image opens in the next view, but is not presented in the frame that I give it. as well as when I click on any image that it removed from the collection view. please tell me how to reload UICollectionViewCells enter image description here Please suggest me to place the image on the given frames. I am sharing my code

- (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];  
}
+4
5

. . .

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];
+1

- , , , .

.

+1

, - , , , , , , . , :

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

1. UIImageVIew . :

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

. , . subviews cellForItemAtIndexPath, . , , . , , UIImageView. , CollectionView :

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

You can replace your expandImage method , inside which you simply get your image with imagearray . And finally, this beautiful library is ready for you, MHFacebookImageViewer .

+1
source

Add imageviewtonavigationController.view

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

I did the above code first added a click gesture then apply the following code

-(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];
    }
0
source

Source: https://habr.com/ru/post/1610534/


All Articles