Domanda

Vorrei aggiungere alcuni angoli arrotondati a tutti i UIImageViews nel mio progetto. Ho già ottenuto il codice di lavoro, ma sto avendo per applicarlo ad ogni immagine; dovrei sottoclasse UIImageView aggiungere questo? Se è così, qualcuno può darmi alcune indicazioni su come fare questo?

Ecco il codice

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *mainpath = [[NSBundle mainBundle] bundlePath];
    welcomeImageView.image = [UIImage imageWithContentsOfFile:[mainpath stringByAppendingString:@"/test.png"]];
    welcomeImageView.layer.cornerRadius = 9.0;
    welcomeImageView.layer.masksToBounds = YES;
    welcomeImageView.layer.borderColor = [UIColor blackColor].CGColor;
    welcomeImageView.layer.borderWidth = 3.0;
    CGRect frame = welcomeImageView.frame;
    frame.size.width = 100;
    frame.size.height = 100;
    welcomeImageView.frame = frame;
}
È stato utile?

Soluzione

Si potrebbe utilizzare una categoria per UIImage che è un modo alternativo per sottoclasse di una classe e, a volte più facile per solo piccoli cambiamenti.

per esempio aggiungere un metodo che restituisce un UIImage con l'angolo arrotondato attributi impostati.

+(UIImage *)imageWithContentsOfFile:(NSString *)file cornerRadius:(NSInteger)... 

più informazioni categorie Objective-C può essere trovato http: // macdevelopertips. com / Objective-C / Objective-C-categories.html

Altri suggerimenti

Controlla questo - Angoli arrotondati su UIImage

La modifica strato sembra essere il modo migliore.

UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];

Invece di sottoclasse, è possibile ottenere più potente funzionalità attraverso semplici categorie su UIImageView e CALayer.

Creare una categoria a UIImageView in questo modo:

- (void)maskRoundCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    // To round all corners, we can just set the radius on the layer
    if ( corners == UIRectCornerAllCorners ) {
        self.layer.cornerRadius = radius;
        self.layer.masksToBounds = YES;
    } else {
        // If we want to choose which corners we want to mask then
        // it is necessary to create a mask layer.
        self.layer.mask = [CALayer maskLayerWithCorners:corners radii:CGSizeMake(radius, radius) frame:self.bounds];
    }
}

Ciò richiede un metodo di categoria su CALayer:

+ (id)maskLayerWithCorners:(UIRectCorner)corners radii:(CGSize)radii frame:(CGRect)frame {

    // Create a CAShapeLayer
    CAShapeLayer *mask = [CAShapeLayer layer];

    // Set the frame
    mask.frame = frame;

    // Set the CGPath from a UIBezierPath
    mask.path = [UIBezierPath bezierPathWithRoundedRect:mask.bounds byRoundingCorners:corners cornerRadii:radii].CGPath;

    // Set the fill color
    mask.fillColor = [UIColor whiteColor].CGColor;

    return mask;
}

Quindi, questo ti permette di arrotondare qualsiasi combinazione (vedi UIRectCorner) di curve, che è particolarmente utile se si vuole mettere l'immagine in uno stile UITableView gruppo. C'è un avvertimento quando si fa questo però. Perché non abbiamo sottoclasse UIImageView, non possiamo iniettare qualsiasi codice in layoutSubviews, il che significa che lo strato di maschera non può essere corretto. Infatti, quando si configurano le cellule, i limiti della visualizzazione dell'immagine non sarà nemmeno essere impostati quando si chiama il metodo categoria. Quindi, è necessario per garantire i confini della vista dell'immagine è impostato prima di aggiungere angoli arrotondati (tranne se si utilizza UIRectCornersAllCorners).

Ecco il codice che fa questo:

        // Perform corner rounding
        UIRectCorner corners = !UIRectCornerAllCorners;
        if (indexPath.row == 0) 
            corners = UIRectCornerTopLeft;
        if (indexPath.row == numberOfRowsInTheTable)  
            corners |= UIRectCornerBottomLeft;

        if (corners > 0) {
            cell.imageView.bounds = CGRectMake(0.f, 0.f, [self.tableView rowHeight], [self.tableView rowHeight]);
            [cell.imageView maskRoundCorners:corners radius:10.f];
        } else {
            [cell.imageView removeRoundCornersMask];
        }

Ho un'altra categoria che elimina gli angoli arrotondati -. Tutto ciò che fa è rimuovere eventuali maschere e impostare il cornerRadius a 0

Sì, si dovrebbe creare una sottoclasse UIImageView, e utilizzare la sottoclasse personalizzata in tutto il progetto.

È possibile creare una sottoclasse UIImageView e poi se attuare il suo setNeedsDisplay Metodo gli angoli arrotondati lavoreranno sottoclassi. (Non dimenticate di importare QuartzCore)

-(void)setNeedsDisplay {
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    [self.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.layer setBorderWidth: 2.0];
}

Prova questo,

coverImage.image = [UIImage imageWithContentsOfFile:@"coverImage.png"]; 
coverImage.layer.masksToBounds = YES;
coverImage.layer.cornerRadius = 10.0;
coverImage.layer.borderWidth = 1.0;
coverImage.layer.borderColor = [[UIColor brown] CGColor];

questo può aiutare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top