2012-07-30 13 views
5

Ho lo stesso problema, come il link qui: Can't add a corner radius and a shadowAggiungi Round Corner al UIImageView e display Ombra effetto

se metto maskToBounds = YES, ottengo angoli arrotondati, ma nessuna ombra Se metto maskToBounds = NO , Ottengo ombra, ma senza angoli arrotondati.

Poi ho seguito le istruzioni in questo collegamento sopra, impostando maskToBounds = NO, "ma piuttosto impostare il raggio dell'angolo e impostare il percorso di Bezier dell'ombra con un retto arrotondato. Mantenere il raggio dei due lo stesso". Ma poi, non arrivo agli angoli arrotondati, né ho alcuna ombra! (cioè Square Image senza Shadow) Potresti per favore aiutarmi? Non so cosa ho fatto di sbagliato. Grazie in anticipo.

self.userImageView.backgroundColor = [UIColor redColor]; 
self.userImageView.clipsToBounds = NO; 
self.userImageView.contentMode = UIViewContentModeCenter; 
self.userImageView.layer.masksToBounds = NO; 

self.userImageView.layer.borderWidth = 1; 
self.userImageView.layer.borderColor = [[UIColor grayColor] CGColor]; 

self.userImageView.layer.shadowOpacity = 1; 
self.userImageView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
self.userImageView.layer.shadowRadius = 8.0f; 
self.userImageView.layer.shadowOffset = CGSizeMake(-3, 0); 
self.userImageView.layer.shouldRasterize = YES; 

self.userImageView.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:[self.userImageView bounds] cornerRadius:10.0f] CGPath]; 

[self addSubview:self.userImageView]; 

risposta

18

Sfortunatamente, non penso che UIImageView supporti angoli arrotondati e ombre allo stesso tempo.

È tuttavia possibile creare l'ombra nella vista eccellente di UIImageView.

CGFloat cornerRadius = 3.0 

UIView *container = [[UIView alloc] initWithFrame:aRect]; 
container.layer.shadowOffset = CGSizeMake(0, 0); 
container.layer.shadowOpacity = 0.8; 
container.layer.shadowRadius = 5.0; 
container.layer.shadowColor = [UIColor redColor].CGColor; 
container.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:container.bounds cornerRadius:cornerRadius] CGPath]; 

self.userImageView.layer.cornerRadius = cornerRadius; 
self.userImageView.layer.masksToBounds = YES; 
self.userImageView.frame = container.bounds; 
[container addSubview:self.userImageView]; 
[self addSubview:container]; 
+0

Penso che il tuo approccio funzionerà, ci proverò presto. Grazie per la risposta. È passato molto tempo da quando ho postato la domanda, ero molto eccitato di vedere una risposta. – John

+0

che cos'è 'aRect' ?? –

+0

aRect è dove desideri che sia il tuo imageView (ovvero la cornice di imageView quando non hai il contenitore.) –

Problemi correlati