UIScrollView 显示第一张图像正常,但后续图像缩放和平移不正确
Posted
技术标签:
【中文标题】UIScrollView 显示第一张图像正常,但后续图像缩放和平移不正确【英文标题】:UIScrollView displays first image fine but subsequent images zoom and pan incorrectly 【发布时间】:2013-03-30 11:26:29 【问题描述】:我的应用程序可以从 Flickr 中提取图像,并且可以在 UIScrollView 中选择并显示它们,并启用缩放和平移。我选择的第一张图像很好,平移和缩放按预期工作。但是,我在第一个图像之后加载的任何图像都无法正常运行。平移是有限的,当我第一次缩放它时,它总是将图像重置到屏幕的左上角,并且再次平移非常有限,但是它确实缩放。方法如下:
-(void)loadImageFromURLandSetupScrollView
[self startToolbarActivityIndicator];
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader", NULL);
dispatch_async(downloadQueue, ^
UIImage *imageFromFlickr = [UIImage imageWithData:[NSData dataWithContentsOfURL:self.photoURL]];
dispatch_async(dispatch_get_main_queue(), ^
self.photo.image = imageFromFlickr;
self.photo.frame = CGRectMake(0, 0, self.photo.image.size.width, self.photo.image.size.height);
self.scrollView.contentSize = self.photo.bounds.size;
//setup the zoom scale to show best possible
float zoomScaleWidth = self.scrollView.bounds.size.width / self.photo.image.size.width;
float zoomScaleHeight = self.scrollView.bounds.size.height / self.photo.image.size.height;
if(zoomScaleHeight < zoomScaleWidth)
self.scrollView.minimumZoomScale = zoomScaleHeight;
self.scrollView.zoomScale = zoomScaleWidth;
else
self.scrollView.minimumZoomScale = zoomScaleWidth; //make sure it can't be zoomed less than longest side at full screen
self.scrollView.zoomScale = zoomScaleHeight;
self.scrollView.maximumZoomScale = 10;
//set scroll view background colour
self.scrollView.backgroundColor = [UIColor blackColor];
[self stopToolBarActivityMonitor];
);
);
【问题讨论】:
【参考方案1】:所以这似乎是由新的自动布局功能引起的,我的解决方法是在故事板中没有 UIImageView,而是在需要时以编程方式将其添加到滚动视图中。
-(UIImageView *)photo
if (!_photo) _photo = [[UIImageView alloc] initWithFrame:CGRectZero];
return _photo;
-(void)loadImageFromURLandSetupScrollView
[self startToolbarActivityIndicator];
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader", NULL);
dispatch_async(downloadQueue, ^
UIImage *imageFromFlickr = [UIImage imageWithData:[NSData dataWithContentsOfURL:self.photoURL]];
dispatch_async(dispatch_get_main_queue(), ^
self.photo.image = imageFromFlickr;
[self.scrollView addSubview:self.photo];
// Set the min and max:
self.scrollView.minimumZoomScale = 0.2;
self.scrollView.maximumZoomScale = 5.0;
self.scrollView.delegate = self;
// Set the content:
self.scrollView.zoomScale = 1.0; // reset zoomScale for new image
self.scrollView.contentSize = self.photo.image.size;
self.photo.frame = CGRectMake(0, 0, self.photo.image.size.width, self.photo.image.size.height);
//setup the zoom scale to show best possible
float zoomScaleWidth = self.scrollView.bounds.size.width / self.photo.image.size.width;
float zoomScaleHeight = self.scrollView.bounds.size.height / self.photo.image.size.height;
if(zoomScaleHeight < zoomScaleWidth)
self.scrollView.zoomScale = zoomScaleWidth;
else
self.scrollView.zoomScale = zoomScaleHeight;
//set scroll view background colour
self.scrollView.backgroundColor = [UIColor blackColor];
[self stopToolBarActivityMonitor];
);
);
【讨论】:
以上是关于UIScrollView 显示第一张图像正常,但后续图像缩放和平移不正确的主要内容,如果未能解决你的问题,请参考以下文章