Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop UIImage to fit a frame image

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.

like image 545
CainaSouza Avatar asked Jan 30 '13 16:01

CainaSouza


2 Answers

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.

like image 188
Pier-Luc Gendreau Avatar answered Oct 09 '22 16:10

Pier-Luc Gendreau


make a IBOutlet in your controller.

@property (retain)IBOutlet UIImageView* imageView; 

and in -(void) viewDidLoad set

imageView.layer.masksToBounds = YES; 
like image 22
Remizorrr Avatar answered Oct 09 '22 17:10

Remizorrr