0

I have 3 scroll views which I added in IB then in my viewDidLoad I call a method that add a UIImageView to each subview. I would like to have zooming on each subview so I use

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{

return [scrollView.subviews objectAtIndex:0];
}

but viewForZoomingInScrollView is getting called before the imageviews are added to the subviews. How can solve this?

This is the loadImages method:

-(void) loadImages{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *imageData1 = [defaults dataForKey:@"image1"];
NSData *imageData2 = [defaults dataForKey:@"image2"];
NSData *imageData3 = [defaults dataForKey:@"image3"];
UIImage *img1 = [UIImage imageWithData:imageData1];
UIImage *img2 = [UIImage imageWithData:imageData2];
UIImage *img3 = [UIImage imageWithData:imageData3];

[self loadImage:img1 toScrollView:scrollViewTop];
[self loadImage:img2 toScrollView:scrollViewLeft];
[self loadImage:img3 toScrollView:scrollViewRight];
}

-(void) loadImage:(UIImage *)image toScrollView:(UIScrollView *)scrollView
{
//scale and rotate image to default
UIImage *scaledImage = scaleAndRotateImage(image);

//create imageview and add image to the view
UIImageView *imageView = [[UIImageView alloc]initWithImage:scaledImage];

CGFloat imageWidth = scaledImage.size.width;
CGFloat imageHeight = scaledImage.size.height;

int scrollWidth = scrollView.frame.size.width;
int scrollHeight = scrollView.frame.size.height;

//Limit by width or height, depending on which is smaller in relation to
//the scrollview dimension.
float scaleX = scrollWidth / imageWidth;
float scaleY = scrollHeight / imageHeight;
float scaleScroll =  (scaleX < scaleY ? scaleY : scaleX);

// scrollView.bounds = CGRectMake(0, 0,imageWidth , imageHeight );
//scrollView.frame = CGRectMake(0, 0, scrollWidth, scrollHeight);

scrollView.delegate = self;

scrollView.contentSize = imageView.frame.size;
scrollView.pagingEnabled = NO;
scrollView.maximumZoomScale = scaleScroll*3;
scrollView.minimumZoomScale = scaleScroll;
scrollView.zoomScale = scaleScroll;

[scrollView addSubview:imageView];

}
7
  • where (in what method) are you adding the suview ImageViews Commented Jun 6, 2012 at 18:39
  • if I add 'NSLog@("%@",[scrollView.subviews objectAtIndex:0]);' after I add the subview it returns the correct data. but the viewForZoomingInScrollView method is called before they are loaded Commented Jun 6, 2012 at 18:43
  • Where are you calling loadImages from? Commented Jun 6, 2012 at 18:43
  • oh, viewDidLoad, that's my question though. Where should it be called? Commented Jun 6, 2012 at 18:44
  • Interestingly, if I were doing something like this, I would have probably defined the UIImageView object within IB adding them into the proper UIScrollView objects graphically. In this way you may be able to get around your problem. You can still bring in the images in code, but it may be easier to simply add your UIImageViews into the ScrollViews in the Interface Builder (especially if you are wanting to deal with a static 3 scrollviews each with a single imageview). Commented Jun 6, 2012 at 18:49

1 Answer 1

1

Try adding the image view as a subview before you configure zooming. Say, before the delegate assignment.

viewForZoomingInScrollView: is getting called by one of the scroll view's setters; looking at the stack trace from there should tell you which one.

Sign up to request clarification or add additional context in comments.

3 Comments

What you're saying sounds right but I'm not sure how to change this. I built the scrollView in Interface Builder and added a UIImageView to each one programmatically. Also, I'm not sure how to stack trace the error. Thanks!
take this line "scrollView.delegate = self;" and put it below "[scrollView addSubview:imageView];" in your loadImage routine is what Wilbur is saying!
Actually I meant move [scrollView addSubview:imageView]; above scrollView.delegate = self; since that moves it above the zoom setters too, but it might work either way. To see the stack trace, add a breakpoint in Xcode by clicking on a line number inside the viewForZoomingInScrollView: method and then run your app. Once the app reaches that line of code, the app will pause and the stack trace will appear in Xcode's left sidebar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.