46

I need to crop a UIImage, taken with the device camera, so that it fits inside another UIImage, which represents a frame (with rounded borders and so on). Check the image below:

enter image description here Using Aspect Fill

enter image description here Using Aspect Fit

So, what I need is to remove the image excess that is out of the frame bounds. I tried using UIBezierPath, CGImageRef and other methods that I Googled but I'm not finding a solution for this.

bneely
  • 9,083
  • 4
  • 38
  • 46
CainaSouza
  • 1,417
  • 2
  • 16
  • 31

4 Answers4

128

In Interface Builder, use the following configuration:

enter image description here

There are two important settings, namely:

  1. Mode: Aspect Fill

  2. Clip Subviews

It can also be done programmatically:

[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setClipsToBounds:YES];

This will correctly fill the view with the image, keep its aspect ratio and hide what doesn't fit.

Pier-Luc Gendreau
  • 13,553
  • 4
  • 58
  • 69
  • 4
    If you need to do this programmatically then use `[imageView setContentMode:UIViewContentModeScaleAspectFill];` `[imageView setClipsToBounds:YES];` – AlexanderZ Jul 22 '14 at 17:29
  • I've been using AVCapture to take photo's and always ended up with a stretch on the UIImageView - after seeing these 2 lines of code.. it's brought my many hours of searching to an end! Thank you so much! I got everything right apart from ClipsToBounds.. why didn't I think of this.. it's so obvious haha! Thanks again dude! – Declan Land Jan 09 '15 at 17:18
  • 1
    Clip Subviews has been renamed **Clip to Bounds** – sonique Oct 09 '18 at 13:50
4

make a IBOutlet in your controller.

@property (retain)IBOutlet UIImageView* imageView;

and in -(void) viewDidLoad set

imageView.layer.masksToBounds = YES;
Remizorrr
  • 2,322
  • 1
  • 17
  • 25
1

In interface Builder, access the Mode menu inside of the detail pane (the fourth one) and choose the right one for your UIImageView (I guess "center" or "aspect fit").

enter image description here

OLD ANSWER:

You can use the contentGravity property of CALayer to make it work

A constant that specifies how the layer's contents are positioned or scaled within its bounds.

       @property(copy) NSString *contentsGravity
Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • Didn't work using "Center"... The image fit all the view, beyond the frame image. Do you have an example of how to use the contentsGravity? – CainaSouza Jan 30 '13 at 16:54
  • This keeps the image aspect but I get some blank areas in the top and bottom of the frame image. – CainaSouza Jan 30 '13 at 16:58
  • It doesn't work. When I posted this, it was with aspect fill. Is there any parameter I need to set in IB, beyond Aspect Fill? – CainaSouza Jan 30 '13 at 17:22
  • maybe you could post some pictures of the results you are getting... it is difficult to follow... – sergio Jan 30 '13 at 17:24
  • I got it using the answer posted in http://stackoverflow.com/questions/603907/uiimage-resize-then-crop Thanks for your help! – CainaSouza Jan 30 '13 at 17:40
-1

Use an UIImageView and do

imageView.contentMode = UIViewContentModeScaleAspectFit;
Ismael
  • 3,927
  • 3
  • 15
  • 23