与UIImageView的UIScrollView部分可见在屏幕上
我有一个UIImageView与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)];
}
我什么都试过,但没有成功!
我认为这是与框架的东西,但我无法弄清楚什么。
任何帮助赞赏
@Pheel很高兴你发现它是由于CGAffineTransformMakeRotation
的形象。这可能会改变坐标。
为什么你不只是做:
... set-up imageView...
imageView.frame = imageScrollView.bounds;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageScrollView addSubview:imageView];
你有很多的代码是多余的,或者在你的方法累积效应( frame/center,contentMode,contentSize ...)的多次连续变化,这使得很难知道是什么导致了什么(可能是滚动视图剪切图像视图,其中心首先基于窗口坐标而不是其超视图坐标,但最后不会,因为你重新定义了它的框架......)。你应该从头开始,用一些简单的代码,具体取决于你想要什么,以及你开始的配置(例如什么是滚动视图框?)。
那么我从我的其他项目的代码,它完美的工作。我不知道为什么现在它不起作用(或者更好,但它不像旧项目中那样)。我会尽力去做你所做的事情,但是代码有一些调整,使得图像处于视图的完美中心(即 – Phillip 2012-07-12 22:28:47
)。我不得不再次设置imageScrollView框架! – Phillip 2012-07-13 15:16:11