2012-06-20 10 views
5

Voglio ritagliare l'immagine nella mia app per iPhone utilizzando qualsiasi percorso chiuso come UIBezierPath. so che è possibile usare il rettangolo ma voglio un ritaglio in una forma diversa. come se fossi una forma usando il tatto e voglio ritagliare quell'immagine così com'è possibile. qualsiasi suggerimento o aiuto. Grazie in anticipo.Voglio ritagliare l'immagine usando qualsiasi percorso chiuso come UIBezierPath. è possibile nell'applicazione iPhone?

+1

non ho mai fatto prima, ma suppongo che è necessario utilizzare mago mascheramento. Prova a leggere questa mela Doc [Guida alla programmazione 2D di quarzo] (https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066- CH212-CJBDIJEE). Forse potresti creare un'immagine trasparente con bezierPath a livello di programmazione o tramite l'interazione dell'utente e quindi utilizzare questa immagine come maschera per l'immagine originale. – Gabriele

+0

Il mascheramento è il modo di ottenere. Dai un'occhiata a questo breve esempio http://mobiledevelopertips.com/cocoa/how-to-mask-an-image.html – msg

risposta

2

È possibile ritagliare l'immagine con uno shapelayer. Per farlo devi creare un percorso che sarà usato come bordi per la tua nuova immagine. Dovresti dare un'occhiata a questo 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]; 

Qualcosa di simile.

+0

Vitaliy1 puoi inserire qui il codice come ritagliare l'immagine con lo shapelayer ..... –

0

// In Code Usa:

-(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;} 
Problemi correlati