0

I am trying to draw an image into a UIView subclass overriding drawRect. This image drawing class needs moving and zooming capabilities, so I made it a subview of a scrollview.

My problem is that my image seems to get cropped, either by the screen or the scrollView bounds (the image is larger than the screen). I don't have a clue why, this seems to e pretty basic stuff.

The view hierarchy in my ViewController looks like this:

View Controller
  View
    ScrollView
      MapView

I've created outlets for the scroll view and the map view (which is the view implementing drawRect).

My ViewController initial setup code for configuring the scroll view looks like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    scrollView.delegate = self; 
    scrollView.contentSize = CGSizeMake(450, 476);
    scrollView.minimumZoomScale = 0.6;
    scrollView.maximumZoomScale = 6.0;
}

My drawRect implementation:

- (void)drawRect:(CGRect)rect
{ 
    UIImage *myImage = [UIImage imageNamed:@"map.jpg"];
    CGRect rectangle = CGRectMake(0,0,450,476);
    [myImage drawInRect:rectangle];
}

The image is draggable and zoomable within the scroll view, but it's cropped at what seems to be the screen bounds.

Any help would be appreciated.

mat
  • 611
  • 2
  • 9
  • 25

2 Answers2

0

Your problem is probably in drawInRect. From documentation of UIImage class:

drawInRect:
Draws the entire image in the specified rectangle, scaling it as needed to fit.

Make that rectangle bigger as you wish and center it in UIImageView. This question is answered here UIScrollView image/photo viewer with paging enabled and zooming

Community
  • 1
  • 1
Josef Rysanek
  • 377
  • 3
  • 12
  • Hi, thanks for your reply. But if i change the code to CGRect rectangle = CGRectMake(0,0,1200,1200); it's still getting cropped. My rect dimensions in the posted code are the actual dimensions of the image. – mat Feb 07 '12 at 10:20
  • Take a look at the link I posted above. There is sample code and WWDC video how to make whole photo application from apple. Search through for moving and zooming. – Josef Rysanek Feb 07 '12 at 10:33
  • Ok, using your links I figured out I needed to set a frame for my mapView, using: CGRect mapFrame; mapFrame.origin = CGPointMake(0, 0); mapFrame.size = CGSizeMake(450, 476); mapView.frame = mapFrame; – mat Feb 07 '12 at 11:30
0

drawRect can't draw outside the rect passed as a parameter to the drawRect method, which is determined by the view frame. Make your view 1200,1200 and make sure that clipToBounds is disabled on all the containing views.

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • Ok so I'm guessing that's what the frame of my view does. Thank you for the information, that makes sense. – mat Feb 07 '12 at 11:42
  • Yes, exactly. Set the view frame (actually it's the view.bounds, technically, but they're usually the same) and drawRect draws the contents of that frame. – Nick Lockwood Feb 07 '12 at 11:58