我有应用程序从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];
});
});
}
答案 0 :(得分:0)
所以看起来这是由新的自动布局功能引起的,我使用的工作是在故事板中没有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];
});
});
}