-1

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

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

由于图像解释了 1000 多个单词,这里是:

红色矩形是显示的奇怪偏移量,而图像应该占据整个视图,但事实并非如此。如您所见,它正在被削减。

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

这是我的代码:

- (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)];

}

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

我认为这是有框架的东西,但我不知道是什么。

任何帮助表示赞赏

4

3 回答 3

2

@Pheel 很高兴您因为CGAffineTransformMakeRotation图像而找到它。这可能会改变坐标。

于 2012-07-13T15:14:45.820 回答
0

尝试更改图像分辨率。另外,您是否尝试在视觉上放置滚动视图,然后将图像视图放置在 Interface Builder 中。完成后,您可以在 ViewDidLoad 等方法中对其进行初始化

于 2012-07-12T21:50:26.400 回答
0

你为什么不这样做:

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

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

于 2012-07-12T22:17:48.527 回答