0

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

1 回答 1

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];
    });
});
}
于 2013-03-30T16:11:24.057 回答