【问题标题】:How to center image in a uiimageview/uiscrollview (pre and post zooming)如何在 uiimageview/uiscrollview 中居中图像(缩放前后)
【发布时间】:2011-09-30 22:34:38
【问题描述】:

我正在尝试使图像居中(相对于 iphone 屏幕),以便在加载视图时,图像是中心宽度/高度...在我的情况下,我的视图加载在像素点 0,0图像本身,如果我缩小图像出现在左上角,而不是屏幕/视图的中间。这是我目前所拥有的:

UIImage *image = [UIImage imageNamed: @"t.bmp"];
self.view.backgroundColor = [UIColor blackColor];
self.mi.frame = CGRectMake(0, 0, image.size.width, image.size.height);
self.mi.contentMode = (UIViewContentModeCenter);
self.mi.backgroundColor = [UIColor blackColor];
[self.mi setImage:image];

/* Draw a line here!!
UIGraphicsBeginImageContext(self.mi.frame.size);
[self.mi.image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 20.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0,0);
CGContextAddLineToPoint(context, 200, 200);    
CGContextMoveToPoint(context, 200,0);
CGContextAddLineToPoint(context, 0, 200);
CGContextStrokePath(context);
self.mi.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
*/

self.expimg.maximumZoomScale = 2.0;
self.expimg.minimumZoomScale = 0.1;
self.expimg.clipsToBounds = YES;
self.expimg.delegate = self;
[self.expimg setContentSize:self.mi.frame.size];

[self.expimg addSubview: self.mi];
[self.view addSubview: self.expimg];

谢谢!

编辑:mi 是 UIImageView 而 expimg 是 UIScrollView(使用的界面生成器)。此外,还定义了 viewForZoomingInScrollView。

【问题讨论】:

    标签: iphone objective-c xcode uiscrollview uiimageview


    【解决方案1】:

    您应该能够通过在初始加载以及缩放级别更改时调整图像和滚动视图的位置和大小来做到这一点。创建一个在viewDidLoad 上调用的方法,然后在UIScrollViewDelegate 方法scrollViewDidZoom: 上再次调用。

    这样的东西应该可以工作(其中 imageView 和 scrollView 是类属性):

    - (void)centerImage {
    
        CGSize screenSize = CGSizeMake(320, 480);
    
        CGSize imageSize = imageView.frame.size;
        imageSize.width = imageSize.width * scrollView.zoomScale;
        imageSize.height = imageSize.height * scrollView.zoomScale;
        CGSize scrollSize = scrollView.frame.size;
    
        CGFloat centerX;
        CGFloat centerY;
        CGFloat left;
        CGFloat top;
    
        if (imageSize.width > screenSize.width) {
            scrollSize.width = imageSize.width;
            centerX = imageSize.width/2;
            left = 0;
        } else {
            scrollSize.width = screenSize.width;
            centerX = screenSize.width/2;
            left = centerX - imageSize.width/2;
        }
    
        if (imageSize.height > screenSize.height) {
            scrollSize.height = imageSize.height;
            centerY = imageSize.height/2;
            top = 0;
        } else {
            scrollSize.height = screenSize.height;
            centerY = screenSize.width/2;
            top = centerY - imageSize.height/2;
        }
    
        CGRect scrollFrame = scrollView.frame;
        scrollFrame.size = scrollSize;
        scrollView.frame = scrollFrame;
    
        CGRect imageFrame = imageView.frame;
        imageFrame.origin = CGPointMake(left, top);
    
        scrollView.contentOffset = CGPointMake(centerX-(imageSize.width/2), centerY-(imageSize.height/2));
    
    }
    

    我没有对此进行测试,所以我的一些数学可能是错误的。无论哪种方式,它都有望让您很好地了解该做什么。

    【讨论】:

    【解决方案2】:

    [scrollView zoomToRect:CGRectMake(x, y, width, height) 动画:YES];会为你工作... rect 应该将这些中心坐标作为原点。

    【讨论】:

      【解决方案3】:

      第 1 步:创建一个图像视图属性,然后在 viewDidLoad 处初始化它并添加到 scrollView:

      self.pictureImage = [[UIImageView alloc]initWithImage:self.picture];
      [_scrollView addSubview:_pictureImage];
      self.pictureImage.alpha = 0;
      _scrollView.delegate = self;
      

      第 2 步:创建 adjustZoomScale 函数以获取最小比例

      - (CGFloat)adjustZoomScale{
      CGFloat scale = self.scrollView.bounds.size.width / self.pictureImage.bounds.size.width;
      CGFloat h = scale * self.pictureImage.bounds.size.height;
      if (h > self.scrollView.bounds.size.height) {
          scale = self.scrollView.bounds.size.height / self.pictureImage.bounds.size.height;
      }
      return scale;
      }
      

      第三步:因为滚动视图的框架会在viewDidAppear更新,所以我们需要在这里调整缩放比例。并设置 alpha 使图像看起来更平滑:

      - (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear: animated];
      _scrollView.maximumZoomScale = 1.0;
      _scrollView.minimumZoomScale = [self adjustZoomScale];
      [_scrollView setZoomScale:_scrollView.minimumZoomScale];
      self.pictureImage.alpha = 1;
      }
      

      第四步:实现viewForZoomingInScrollView(滚动委托方法)并返回图片视图

      - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
      return self.pictureImage;
      }
      

      第 5 步:实现 scrollViewDidZoom(滚动委托方法)并调整图像视图的点

      - (void)scrollViewDidZoom: (UIScrollView*) scrollView {
      CGSize boundsSize = scrollView.bounds.size;
      CGRect contentsFrame = _pictureImage.frame;
      
      if (contentsFrame.size.width < boundsSize.width) {
          contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0;
      } else {
          contentsFrame.origin.x = 0.0;
      }
      
      if (contentsFrame.size.height < boundsSize.height) {
          contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0;
      } else {
          contentsFrame.origin.y = 0.0;
      }
      _pictureImage.frame = contentsFrame;
      }
      

      感谢 this post 的 shawhu 在缩放时调整图像视图的点

      【讨论】:

        猜你喜欢
        • 2017-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-23
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        相关资源
        最近更新 更多