How to erase part of UIImage

Is there any way to erase the black background for this image alt text

I found this sample code found in this thread , but I don't understand how this works.

- (void)clipImage {
  UIImage *img = imgView.image;
  CGSize s = img.size;
  UIGraphicsBeginImageContext(s);
  CGContextRef g = UIGraphicsGetCurrentContext();
  CGContextAddPath(g,erasePath);
  CGContextAddRect(g,CGRectMake(0,0,s.width,s.height));
  CGContextEOClip(g);
  [img drawAtPoint:CGPointZero];
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
}

Maybe I'm wrong.

Regards,
kl94

+2
source share
2 answers

The code uses quartz (iPhone graphics engine) to clip the image. some details:

UIGraphicsBeginImageContext(s);
CGContextRef g = UIGraphicsGetCurrentContext();

First you need some kind of “target” for drawing. In graphics, this is usually called context. Above, you tell the system that you need an image context (bitmap) with a given size, and then you get a link to it.

CGContextAddPath(g,erasePath);
CGContextAddRect(g,CGRectMake(0,0,s.width,s.height));
CGContextEOClip(g);

, . , " ".

[img drawAtPoint:CGPointZero];

. , .

imageView.image = UIGraphicsGetImageFromCurrentImageContext();

, (),

: , , .

+4

, "erasePath". CGRect, , "cropRect" -

CGMutablePathRef erasePath = CGPathCreateMutable();
CGPathAddRect(erasePath, NULL, cropRect);
CGContextAddPath(g,erasePath);
0

All Articles