2012-06-20 13 views
5

quiero recortar la imagen en mi aplicación de iPhone utilizando cualquier ruta cercana como UIBezierPath. Sé que es posible usar un rectángulo, pero quiero que el cultivo tenga una forma diferente. como hago una forma con el tacto y quiero recortar esa imagen para saber cómo es posible. cualquier sugerencia o ayuda. Gracias de antemano.Quiero recortar imágenes usando cualquier ruta cercana como UIBezierPath. es posible en la aplicación de iPhone?

+1

nunca lo hice antes, pero supongo que es necesario utilizar enmascaramiento mago. Intente leer este Apple Doc [Guía de programación de Quartz 2D] (https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066- CH212-CJBDIJEE). Tal vez podría crear una imagen transparente con bezierPath programáticamente o por interacción del usuario y luego usar esta imagen como máscara para su imagen original. – Gabriele

+0

El enmascaramiento es la forma de obtenerlo. Eche un vistazo a este pequeño ejemplo http://mobiledevelopertips.com/cocoa/how-to-mask-an-image.html – msg

Respuesta

2

Puede recortar su imagen con una capa de forma. Para hacerlo, debes crear una ruta que se usará como bordes para tu nueva imagen. Debería mirar esto 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]; 

Algo como esto.

+0

Vitaliy1 ¿Puede poner aquí el código de cómo recortar la imagen con la capa de forma ..... –

0

// En Código Uso:

-(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;} 
Cuestiones relacionadas