Hello all !
To crop an UIImage in iOS the following code can be used :-
 
 - (void)viewDidLoad {
    [super viewDidLoad];
    
   [_originalImage setImage:[UIImage imageNamed:@"Scenery.jpg"]];
    
    CGRect rect = CGRectMake(_originalImage.frame.size.width / 4, _originalImage.frame.size.height / 4 , (_originalImage.frame.size.width / 2), (_originalImage.frame.size.height / 2));
   
    CGImageRef imageRef = CGImageCreateWithImageInRect([_originalImage.image CGImage], rect);
    
    UIImage *img = [UIImage imageWithCGImage:imageRef];
 
    [_croppedImage setImage:img];
}
 
In the above code, _originalImage is the outlet of the UIImageView in which the full image will be displayed and _croppedImage is the outlet on cropped image. We are setting a rectangle area from the original image. CGImageCreateWithImageInRect is used for creating bitmap image within the subregion of the existing image.
 
The output will be as :-
 

 
                                
                       
                    
0 Comment(s)