2012-04-06 3 views
0

У меня есть изображение в формате JPG с круглым объектом в прямоугольнику и хочу сделать envoirement круглого объекта прозрачного ...IOS: Inverse UIBezierPath (bezierPathWithOvalInRect)

Example

(Удалить красную области в этот пример)

С помощью этого iOS make part of an UIImage transparent и «UIBezierPath bezierPathWithOvalInRect» у меня есть небольшой успех, но это делает путь вокруг моего объекта, и мне нужно его инвертировать, чтобы сделать снаружи, а не внутри пути прозрачный ..

Im не уверен, где я должен изменить свой код, чтобы получить правильный результат ..

Вот мой код:

//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; 

Любого получил разрешающий?

+0

ли вы только хотите сделать красный стать прозрачным или черным стать прозрачным? –

ответ

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; 
+0

Эй, это полностью работает, спасибо за ваш быстрый ответ! – Jones123

+0

Можно скопировать текущий контекст в UIBezierPath с помощью '[bezierPath addClip]', так что вам не нужно напрямую играть с 'CGContextRef'. – Dondragmer

+0

безупречный. ;) благодаря –

Смежные вопросы