Voici une méthode que j'utilise dans mes projets pour créer des masques circulaires (ce n'est pas à Swift mais facilement traduisible):
- (UIImage *)circularOverlayMask
{
// Constants
CGRect bounds = self.navigationController.view.bounds;
CGFloat width = bounds.size.width;
CGFloat height = bounds.size.height;
CGFloat diameter = width - (INNER_EDGE_INSETS * 2);
CGFloat radius = diameter/2;
CGPoint center = CGPointMake(width/2, height/2);
// Create the image context
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0);
// Create the bezier paths
UIBezierPath *clipPath = [UIBezierPath bezierPathWithRect:bounds];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(center.x - radius, center.y - radius, diameter, diameter)];
[clipPath appendPath:maskPath];
clipPath.usesEvenOddFillRule = YES;
[clipPath addClip];
[[UIColor colorWithWhite:0 alpha:0.5f] setFill];
[clipPath fill];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
Je crée essentiellement un sous-vue que j'ajouter au-dessus de l'image ScrollView, comme celui-ci :
UIImageView *maskView = [[UIImageView alloc] initWithImage:[self overlayMask]];
maskView.userInteractionEnabled = NO;
[self.view insertSubview:maskView aboveSubview:_scrollView];
Espérons que ça aide.
(à l'origine dans DZNPhotoPickerController)
Il fonctionne très bien! Merci Cyril tu es au top;) – Sam
Merci Cyril, savez-vous comment puis-je créer un objet image avec ce qui est montré dans le cercle (dans le cadre d'une image plus grande qui se trouve derrière le masque)? – Sam
Vous devriez jeter un oeil à l'implémentation de DZNPhotoPickerController dans la méthode '' 'editedImage''': https://github.com/dzenbot/DZNPhotoPickerController/blob/3bba17c70b29ca44313b30cff9b4eefe3bc82389/Source/Classes/Editor/DZNPhotoEditorViewController.m#L481 – Cyril