I want to crop the image using any close path like UIBezierPath. Is this possible in the iPhone app?

I want to crop the image in my iPhone application using any close path like UIBezierPath. I know that you can use a rectangle, but I want the culture to be in a different shape. for example, I make one shape with a touch and want to crop this image as much as possible. any suggestion or help. Thanks in advance.

+5
source share
3 answers

You can crop the image using the template. To do this, you need to create a path that will be used as borders for your new image. You should look at the question .

CALayer* contentLayer = [CALayer layer];
[contentLayer setFrame:CGRectMake(0, 0, 80, 80)];
CAShapeLayer* mask = [CAShapeLayer layer];

CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, NULL, 10, 10);
CGPathAddLineToPoint(path, NULL, 10, 80);
CGPathAddLineToPoint(path, NULL, 80, 80);
CGPathAddLineToPoint(path, NULL, 80, 10);
mask.path = path;

[contentLayer setContents:(id)[[UIImage imageNamed:@"image.png"] CGImage]];
[contentLayer setMask:mask];          

[[self layer]addSublayer:contentLayer];

Something like that.

+2
source

You can accomplish this with a mask. You can create a mask with CGImageMaskCreate.

if you need examples:

Any idea why this image masking code is not working?

http://mobiledevelopertips.com/cocoa/how-to-mask-an-image.html

+1
source

// :

  -(UIImage *)croppedImage

{
   UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
[self.bezierPath closePath];

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0,   0.0, 0.0);
_b_image = self.bg_imageview.image;

CGSize imageSize = _b_image.size;
CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);

UIGraphicsBeginImageContextWithOptions(imageSize, NO, [[UIScreen mainScreen] scale]);
[self.bezierPath addClip];
[_b_image drawInRect:imageRect];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;}
0

All Articles