TTPhotoViewController 在不同interfaceOrientation下的显示问题

本文探讨了在使用Three20库的TTPhotoViewController显示照片时,遇到的问题及解决方案。在竖屏显示时,图片自动放大并可能显示错误内容;而在横屏时,图片显示过小。通过修改代码,实现根据屏幕方向调整图片显示模式,确保在不同旋转方向下都能正确显示照片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        最近在使用Three20库进行开发,并使用了TTPhotoViewController 来显示照片,不清楚大家是否遇到过我这样的问题:直接TTPhotoViewController ,使用在竖屏的时候,图片显示是错误了,此时图片上自动放大,且经常会显示到其他页的图片;而在横屏的情况下是正确的。

        参考了网上一些解决方法:将TTPhotoView.m的- (void)setImage:(UIImage*)image函数中的UIViewContentModeScaleAspectFill改为UIViewContentModeScaleAspectFit,依照该方法修改后,此时发现竖屏是正确的,但是横屏确显示的是小图,无法满屏显示。

       由此得出,在不同的interfaceOrientation下,需设置不同的contentmode。因此经过实验,修改代码如下:

TTPhotoViewController .m

增加函数:updatePhotoViewContentMode:(UIInterfaceOrientation)interfaceOrientation

///
- (void)updatePhotoViewContentMode:(UIInterfaceOrientation)interfaceOrientation
{
    TTPhotoView* centerPhotoView = self.centerPhotoView;
    UIViewContentMode currentContentMode;
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
        currentContentMode = UIViewContentModeScaleAspectFill;
    }
    else{
        currentContentMode = UIViewContentModeScaleAspectFit;
    }
    //先设置非当前view到contentMode,防止旋转时,会看到非centerPhotoview
    for (TTPhotoView* photoView in _scrollView.visiblePages.objectEnumerator) {
        if (centerPhotoView == photoView)
            break;
        photoView.contentMode = currentContentMode;
    }
    centerPhotoView.contentMode = currentContentMode;
}

修改代码:

///
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                         duration:(NSTimeInterval)duration {
  [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
  [self updateToolbarWithOrientation:toInterfaceOrientation];
  [self updatePhotoViewContentMode:toInterfaceOrientation];
}

///
- (void)loadImages {
  TTPhotoView* centerPhotoView = self.centerPhotoView;
  for (TTPhotoView* photoView in _scrollView.visiblePages.objectEnumerator) {
    if (photoView == centerPhotoView) {
      [photoView loadPreview:NO];

    } else {
      [photoView loadPreview:YES];
    }
    //修改interfaceOrientation
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
      photoView.contentMode = UIViewContentModeScaleAspectFill;
    }
    else{
      photoView.contentMode = UIViewContentModeScaleAspectFit;
    }
  }

  if (_delayLoad) {
    _delayLoad = NO;
    [self startImageLoadTimer:kPhotoLoadLongDelay];

  } else {
    [centerPhotoView loadImage];
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值