2013-12-08 12 views
5

codice attuale:Maschera dimensioni arbitrarie UIImageView con UIImage ridimensionabile maschera

self.backgroundImageView.image = [self.message imageOfSize:self.message.size]; // Random image, random size 

    UIImage *rightBubbleBackground = [[UIImage imageNamed:@"BubbleRight"] 
    resizableImageWithCapInsets:BubbleRightCapInsets 
    resizingMode:UIImageResizingModeStretch]; 

    CALayer *mask = [CALayer layer]; 
    mask.contents = (id)[rightBubbleBackground CGImage]; 

    mask.frame = self.backgroundImageView.layer.frame; 
    self.backgroundImageView.layer.mask = mask; 
    self.backgroundImageView.layer.masksToBounds = YES; 

Questo non funziona correttamente. Sebbene la maschera sia applicata, il valore rightBubbleBackground non viene ridimensionato correttamente per adattarsi allo self.backgroundImageView, anche se è stato impostato il ridimensionamento degli inserimenti cap (BubbleRightCapInsets).

Immagine originale:

Original image

immagine Mascherina (rightBubbleBackground):

Mask

Risultato:

Result

Ho trovato this answer ma funziona solo per immagini simmetriche. Forse potrei modificare quella risposta per il mio uso.

+0

penso 'mask.frame = self.backgroundImageView.layer.frame; 'dovrebbe essere' mask.frame = self.backgroundImageView.layer.bounds; '. L'uso del codice comporterà uno spostamento della maschera se la cornice del livello non è uguale ai limiti. – PowerQian

risposta

10

Ho sbagliato. Questa risposta può essere modificata per funzionare con immagini asimmetriche. Ho lavorato un po 'su that answer e ho risolto il mio problema.

Il seguente codice ha reso la mia inserti Lavoro Cap per lo strato di maschera:

mask.contentsCenter = 
CGRectMake(BubbleRightCapInsets.left/rightBubbleBackground.size.width, 
BubbleRightCapInsets.top/rightBubbleBackground.size.height, 
1.0/rightBubbleBackground.size.width, 
1.0/rightBubbleBackground.size.height); 

Risultato:

Result

+1

Amico. GRAZIE!!! – Mahouk

+0

qual è il valore di BubbleRightCapInsets .. io uso questo UIEdgeInsets BubbleRightCapInsets = UIEdgeInsetsMake (15.0f, 20.0f, 15.0f, 20.0f); –

+0

@MANCHIKANTIKRISHNAKISHORE Da allora ho ridisegnato l'app e non ricordo più i riquadri. Dovresti usare un valore più grande per 'right', per tenere conto della freccia, e forse anche di 'bottom'. – duci9y

0

ho avuto (in parte) lo stesso problema - cioè il contenuto del livello pixelated . Per me è stato risolto impostando il valore contentScale sul CALayer - per qualche motivo la scala di default su CALayers è sempre 1.0, anche sui dispositivi Retina.

cioè

layer.contentsScale = [UIScreen mainScreen].scale; 

Inoltre, se si sta disegnando una forma utilizzando CAShapeLayer e chiedendosi suoi bordi Guardiamo un po 'frastagliata sui dispositivi retina, provare:

shapeLayer.rasterizationScale = [UIScreen mainScreen].scale; 
shapeLayer.shouldRasterize = YES;