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
    • 515
    Comment on it

    Hello all !
    To crop an UIImage in iOS the following code can be used :-

     

    1. - (void)viewDidLoad {
    2. [super viewDidLoad];
    3. [_originalImage setImage:[UIImage imageNamed:@"Scenery.jpg"]];
    4. CGRect rect = CGRectMake(_originalImage.frame.size.width / 4, _originalImage.frame.size.height / 4 , (_originalImage.frame.size.width / 2), (_originalImage.frame.size.height / 2));
    5. CGImageRef imageRef = CGImageCreateWithImageInRect([_originalImage.image CGImage], rect);
    6. UIImage *img = [UIImage imageWithCGImage:imageRef];
    7. [_croppedImage setImage:img];
    8. }
    9.  

     

    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
Reset Password
Fill out the form below and reset your password: