Search code examples
objective-cuiscrollviewuiimageview

UIScrollView with UIImageView partially visible on screen


I have a UIScrollView with a UIImageView in order to enable zoom features.

The problem now is that the scrollView should be shown fullscreen (except the navigationBar), but it's shown with a weird "offset".

Due that images explains more than 1000 words, here it is:

The red rectangles are the weird offset that's being shown, while the image should take the whole view, but it's not. As you see,it's being cut instead.

I tried changing the frame, bounds etc but same result.

Here's my code:

- (void)viewDidLoad
{   [super viewDidLoad];

    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

    imageScrollView.bouncesZoom = YES;
    imageScrollView.delegate = self;
    imageScrollView.clipsToBounds = NO; // If set to yes, the image would be like the screen above
    imageView = [[UIImageView alloc] init];
    imageView.clipsToBounds = YES;


    imageScrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);

    //activity indicator
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    activityIndicator.hidesWhenStopped = YES;
    activityIndicator.hidden = NO;
    activityIndicator.center = CGPointMake(self.imageView.frame.size.width /2, self.imageView.frame.size.height/2);
    [activityIndicator startAnimating];


    [imageView setImageWithURL:[NSURL URLWithString:tipImageString]
              placeholderImage:nil options:SDWebImageProgressiveDownload
                       success:^(UIImage *image) { [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }
                       failure:^(NSError *error) {  [activityIndicator stopAnimating];[activityIndicator removeFromSuperview]; }];
    [imageView addSubview:activityIndicator];

    imageView.userInteractionEnabled = YES;
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);
    imageView.center = [[imageScrollView window] center];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [imageScrollView setContentMode:UIViewContentModeScaleAspectFit];
    [imageScrollView addSubview:imageView];
    imageScrollView.contentSize = self.imageView.image.size;    
    imageScrollView.decelerationRate = UIScrollViewDecelerationRateFast;

    CGSize boundsSize = self.imageScrollView.bounds.size;
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    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;

    // calculate minimum scale to perfectly fit image width, and begin at that scale
    float minimumScale = 0.50;//[imageScrollView frame].size.width  / [imageView frame].size.width;
    imageScrollView.maximumZoomScale = 5.0;
    imageScrollView.minimumZoomScale = minimumScale;
    imageScrollView.zoomScale = minimumScale;
    [imageScrollView setContentMode:UIViewContentModeScaleAspectFit];
    [imageView sizeToFit];

    [imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

}

I tried everything, but with no success!

I think that's something with frames but i can't figure out what.

Any help appreciated


Solution

  • @Pheel Glad that you find it due to CGAffineTransformMakeRotation of the image. This may change the coordinates.