I want to crop an image which is on the UIImageview into any shape
Asked
Active
Viewed 1,925 times
2
-
This may help u.. [ http://stackoverflow.com/questions/6305056/cropping-image-in-iphone/12197457#12197457 ] – Nookaraju Sep 05 '12 at 11:38
2 Answers
2
You set the clipping path and voila:
// Load image thumbnail
NSString *imageName = [self.picArray objectAtIndex:indexPath.row];
UIImage *image = [UIImage imageNamed:imageName];
CGSize imageSize = image.size;
CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
// Create the clipping path and add it
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:imageRect cornerRadius:5.0f];
[path addClip];
[image drawInRect:imageRect];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This code loads an image and creates a path by rounding the rectangle, the result is that the final image has been clipped, i.e. rounded corners. RoundedImage is the result.

Sverrisson
- 17,970
- 5
- 66
- 62
0
You can use a CGImageMask
.
A sample exists in the class QuartzMaskingView of Apple's QuartzDemo.

justin
- 104,054
- 14
- 179
- 226