带有UIImageView的UIScrollView在屏幕上部分可见

时间:2012-07-12 21:03:38

标签: objective-c uiscrollview uiimageview

我有一个带UIImageView的UIScrollView以启用缩放功能。

现在的问题是scrollView应该全屏显示(除了navigationBar),但它显示有一个奇怪的“偏移”。

由于图像解释超过1000个单词,这里是: http://i47.tinypic.com/25pprg3.png

红色矩形是显示的奇怪偏移,而图像应该是整个视图,但事实并非如此。正如你所看到的那样,它被削减了。

我尝试更改框架,边界等但结果相同。

这是我的代码:

- (void)viewDidLoad
{   [super viewDidLoad];

    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

    imageScrollView.bouncesZoom = YES;
    imageScrollView.delegate = self;
    imageScrollView.clipsToBounds = NO; // If set to yes, the image would be like the screen above
    imageView = [[UIImageView alloc] init];
    imageView.clipsToBounds = YES;


    imageScrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);

    //activity indicator
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    activityIndicator.hidesWhenStopped = YES;
    activityIndicator.hidden = NO;
    activityIndicator.center = CGPointMake(self.imageView.frame.size.width /2, self.imageView.frame.size.height/2);
    [activityIndicator startAnimating];


    [imageView setImageWithURL:[NSURL URLWithString:tipImageString]
              placeholderImage:nil options:SDWebImageProgressiveDownload
                       success:^(UIImage *image) { [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }
                       failure:^(NSError *error) {  [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }];
    [imageView addSubview:activityIndicator];

    imageView.userInteractionEnabled = YES;
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);
    imageView.center = [[imageScrollView window] center];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [imageScrollView setContentMode:UIViewContentModeScaleAspectFit];
    [imageScrollView addSubview:imageView];
    imageScrollView.contentSize = self.imageView.image.size;    
    imageScrollView.decelerationRate = UIScrollViewDecelerationRateFast;

    CGSize boundsSize = self.imageScrollView.bounds.size;
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    CGRect frameToCenter = imageView.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    imageView.frame = frameToCenter;

    // calculate minimum scale to perfectly fit image width, and begin at that scale
    float minimumScale = 0.50;//[imageScrollView frame].size.width  / [imageView frame].size.width;
    imageScrollView.maximumZoomScale = 5.0;
    imageScrollView.minimumZoomScale = minimumScale;
    imageScrollView.zoomScale = minimumScale;
    [imageScrollView setContentMode:UIViewContentModeScaleAspectFit];
    [imageView sizeToFit];

    [imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

}

我尝试了一切,但没有成功!

我认为这是框架的东西,但我无法弄清楚是什么。

任何帮助表示赞赏

3 个答案:

答案 0 :(得分:2)

@Pheel很高兴你发现它是由于CGAffineTransformMakeRotation的图像。这可能会改变坐标。

答案 1 :(得分:0)

尝试更改图像分辨率。此外,您是否尝试直观地放置滚动视图,然后将图像视图放在Interface Builder中。完成后,您可以使用ViewDidLoad

等方法对其进行初始化

答案 2 :(得分:0)

你为什么不这样做:

... set-up imageView...
imageView.frame = imageScrollView.bounds;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageScrollView addSubview:imageView];

你的方法中有很多冗余或累积效应的代码(帧/中心,contentMode,contentSize等多次连续更改),这使得很难知道是什么导致了什么(可能是滚动视图是剪切图像视图,其中心首先基于窗口坐标而不是其超视图坐标,但最终不是,因为您重新定义了它的框架...)。你可能应该从头开始使用一些更简单的代码,具体取决于你想要什么,以及你开始的配置(例如滚动视图框架是什么?)。