I used the Deepak and Apples solutions and subclassed the UIScrollView with the UIImageView inside it. However, I only zoom in on the first layout of my scrollview subclass, and only if the frame size is smaller than the image size:
- (void)layoutSubviews { [super layoutSubviews]; // center the image as it becomes smaller than the size of the screen CGSize boundsSize = self.bounds.size; 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; // If image is bigger than frame, we zoom it on first load (zoom value is smaller than 1.0) if (_firstLayout) { _firstLayout = NO; self.zoomScale = (self.frame.size.width < _imageView.image.size.width) ? (self.frame.size.width / _imageView.image.size.width) : 1.0; } }
This is how I run my subclass:
- (id)initWithFrame:(CGRect)frame image:(UIImage*)image { self = [super initWithFrame:frame]; if (self) { _firstLayout = YES; _imageView = [[UIImageView alloc] initWithImage:image]; [self addSubview:_imageView]; self.backgroundColor = [UIColor blackColor]; self.minimumZoomScale = 0.1; self.maximumZoomScale = 10; self.delegate = self; } return self; }
wzbozon
source share