Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Crop UIImage in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 483
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: