Skip to content

Instantly share code, notes, and snippets.

@robinmonjo
Created August 6, 2015 20:20
Show Gist options
  • Save robinmonjo/a591650d5a711f7abd9a to your computer and use it in GitHub Desktop.
Save robinmonjo/a591650d5a711f7abd9a to your computer and use it in GitHub Desktop.
Custom image marker

Les markers ne peuvent être que des UIImage

Nous on veut une couleur dynamique et un label dynamique. Donc pour ça on a besoin d'une methode static (genre dans un fichier swift ADMarkerGenerator):

func generateMarkerWithColor(color: UIColor, andText text: String) -> UIImage

Et dans cette méthode, on aura un code comme ça: (écrit en obj-c, pas testé mais ça devrait fonctionner):

#import <QuartzCore/QuartzCore.h> //a ajouter dans le bridging header, pas sur que ça soit necessaire cependant


UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; // a voir pour la taille
view.backgroundColor = [UIColor clearColor];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
UIImage *markerImage = [UIImage imageNamed:@"some_image"]; //donc la il faut utiliser un icone stylé
imageView.image = [markerImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[imageView setTintColor:[UIColor redColor]]; //donc la on va pouvoir changer la couleur de notre icone :)

//ensuite on place un label (donc le numéro de la step)
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"10";
[label sizeToFit];
label.center = CGPointMake(x, y); //avec x et y qui permettent de placer le number ou on veut

[view addSubview:imageView];
[view addSubview:label]; // donc la notre view contient notre marker complet

//on transforme notre view en UIImage
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//ici img peut-être utilisé directe dans un marker custom
return img;

Inspiré par: http://stackoverflow.com/questions/4334233/how-to-capture-uiview-to-uiimage-without-loss-of-quality-on-retina-display

et ça: http://stackoverflow.com/questions/12872680/changing-uiimage-color

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment