Home > DeveloperSection > Forums > How to crop image in rectangle shape in iOS?
Sanjay Sharma
Sanjay Sharma

Total Post:45

Points:315
Posted on    January-20-2016 10:53 PM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 244  View(s)
Rate this:

I am new in iPhone app development, so I have getting problem on image cropping.
I want to crop an image in rectangle shape, I google it and find some solution for that and using this code:

CGRect cropRect = CGRectMake(0, 0, 70, 70);
CGImageRef imageRef = CGImageCreateWithImageRect(self.OriginalImage, cropRect);

but I don't understand that how can I display imageRef into our view controller.
Please help me.



Tarun Kumar

Total Post:210

Points:1614
Supported
Posted on    January-20-2016 11:21 PM

For cropping the image one thing is very important to know that if we want to crop image then we need to convert that image into CGImage after that we can do anything with that image. after cropping the image in result cropped image will be saved into the CGImageRef reference variable.

Now, if we want to add cgimage into any view then we need to convert it into UIImage. below we are provided the complete code:

UIImage croppedImage = nil;
CGRect cropRect = CGRectMake(0, 0, 70, 70);
CGImageRef imageRef = CGImageCreateWithImageRect(self.OriginalImage, cropRect);
// this is the code which is responsible for converting CGImage into UIImage
croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

I hope it will be helpful for you.


Modified On Jan-20-2016 11:23:29 PM

Don't want to miss updates? Please click the below button!

Follow MindStick