0

長方形に丸いオブジェクトを含むjpg画像があり、丸いオブジェクトの環境を透明にしたい...

例

(この例では赤い領域を削除します)

このiOSの助けを借りて、UIImageの一部を透明にし、「UIBezierPath bezierPathWithOvalInRect」で少し成功しましたが、これでオブジェクトの周りにパスが作成され、オブジェクトの内側ではなく外側を作成するために、オブジェクトを反転する必要があります。パスは透過的です。

正しい結果を得るためにコードをどこで変更しなければならないのかわからない。

これが私のコードです:

//BezierPath
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];  

// Create an image context containing the original UIImage.
UIGraphicsBeginImageContext(_imgTemp.image.size);
[_imgTemp.image drawAtPoint:CGPointZero];

// Clip to the bezier path and clear that portion of the image.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context,bezierPath.CGPath);
CGContextClip(context);
CGContextClearRect(context,CGRectMake(0,0,self._imgTemp.image.size.width,self._imgTemp.image.size.height));

// Build a new UIImage from the image context.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self._imgTemp.image = newImage;

誰かが解決しましたか?

4

1 に答える 1

9

黒い円の中にのみ描画するには[_imgTemp.image drawAtPoint:CGPointZero];、後に移動して呼び出しを完全CGContextClip(context);に削除する必要があります。CGContextClearRect( ... )

//BezierPath
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];  

// Create an image context containing the original UIImage.
UIGraphicsBeginImageContext(_imgTemp.image.size);

// Clip to the bezier path and clear that portion of the image.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context,bezierPath.CGPath);
CGContextClip(context);

// Draw here when the context is clipped
[_imgTemp.image drawAtPoint:CGPointZero];

// Build a new UIImage from the image context.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self._imgTemp.image = newImage;
于 2012-04-06T10:31:34.777 に答える