Ios 如何以黑色背景显示全屏图像?

Ios 如何以黑色背景显示全屏图像?,ios,objective-c,Ios,Objective C,我实现了在点击时显示全屏图像的功能。但是,这只会放大图像,但不会以黑色背景显示图像(视图的其他元素仍在背景中)。我该怎么做 //Setup touch up inside of the imageview (in viewdidload) userPictImageView.userInteractionEnabled = YES; userPictImageView.contentMode = UIViewContentModeScaleAspectFit; UITap

我实现了在点击时显示全屏图像的功能。但是,这只会放大图像,但不会以黑色背景显示图像(视图的其他元素仍在背景中)。我该怎么做

//Setup touch up inside of the imageview (in viewdidload)
    userPictImageView.userInteractionEnabled = YES;
    userPictImageView.contentMode = UIViewContentModeScaleAspectFit;
    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewFullScreenTouchHandler:)];
    tapper.numberOfTapsRequired = 1;
    [userPictImageView addGestureRecognizer:tapper];

//present a full screen image (without black background)
-(IBAction)imageViewFullScreenTouchHandler:(id)sender {
    //goto new ViewController and set the image
    UIImageView *imageView = userPictImageView;

    if (!imageIsFullScreen) {
         [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{

         //save previous frame
         prevFrame = imageView.frame;

        blackView=[[UIView alloc]initWithFrame:self.view.frame];
        [blackView setBackgroundColor:[UIColor blackColor]];
        [self.view addSubview:blackView];

         CGRect frame = self.view.frame;
         [imageView setFrame:frame];
         [blackView addSubview:imageView]; }
             completion:^(BOOL finished){
                    imageIsFullScreen = TRUE;
          }];
        return;
     }
    else{
       self.view.backgroundColor = [UIColor clearColor];

        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
          [imageView setFrame:prevFrame];
                }completion:^(BOOL finished){
                    imageIsFullScreen = FALSE;
                }];
         return;
       }

}

您可以更改为UIViewContentModeScaleToFill,或者更好的方法是,构建一个具有黑色背景的UIView,其框架与视图框架匹配。使图像视图成为该黑色视图的子视图

我会这样做。创建一个我可以调用的方法来缩放和取消缩放imageView。要缩放,请构建背景视图并附加图像。要取消缩放,请执行相反的操作,然后销毁背景视图

由于背景视图将是全屏的,请在其上附加一个手势识别器,以便用户可以点击以关闭

- (void)setUserPictImageViewZoomed:(BOOL)zoom animated:(BOOL)animated {

    UIView *blackView = [self.view viewWithTag:128];
    BOOL isZoomed = blackView != nil;

    // if our current state (isZoomed) matches the desired state (zoomed), then we're done
    if (isZoomed == zoom) return;
    NSTimeInterval duration = (animated)? 0.3 : 0.0;

    if (zoom) {
        blackView = [[UIView alloc] initWithFrame:self.view.frame];
        blackView.backgroundColor = [UIColor blackColor];
        blackView.tag = 128;
        blackView.alpha = 0.0;
        [self.view addSubview:blackView];

        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(unzoom:)];
        [blackView addGestureRecognizer:tapGR];

        UIImageView *zoomImageView = [[UIImageView alloc] initWithImage:self.userPictImageView.image];
        zoomImageView.contentMode = UIViewContentModeScaleAspectFit;
        zoomImageView.tag = 129;
        zoomImageView.frame = [self.userPictImageView convertRect:self.userPictImageView.frame toView:blackView];
        [blackView addSubview:zoomImageView];
        [UIView animateWithDuration:duration animations:^{
            zoomImageView.frame = blackView.bounds;
            blackView.alpha = 1.0;
        }];
    } else {
        UIImageView *zoomImageView = (UIImageView *)[blackView viewWithTag:129];
        [UIView animateWithDuration:duration animations:^{
            zoomImageView.frame = self.userPictImageView.frame;
            blackView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [blackView removeFromSuperview];
        }];
    }
}


- (void)unzoom:(UIGestureRecognizer *)gr {
    [self setUserPictImageViewZoomed:NO animated:YES];
}

我在一个模拟器中快速测试了它,它看起来非常流畅。

我正在尝试实现它(我在问题中添加了代码)。但是我怎样才能删除视图并将图像恢复到原始位置呢?谢谢你的提示。我想我在viewdidload中设置了blackview,然后添加/删除它?还是隐藏/展示它?如何从blackView中添加和删除UIImageView?如果您能为我提供一个if(imageIsFullScreen)和else()的基本示例,那将非常有帮助。谢谢!那正是我要找的!我还有一个额外的问题,如果我想使用图像的滚动视图,我应该更改/添加什么?保留所选图像的属性,并使用该属性而不是userPictImageView。由于滚动视图偏移是任意的,因此可能需要对帧进行一些调整。这可能需要一个新的问题。