我有从 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];
});
});
}