mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-29 04:35:36 +08:00
Added support for custom color and image for map annotations
Summary: public This diff extends RCTMap annotations with an `image` and `tintColor` property, which can be used to render completely custom pin graphics. The tintColor applies to both regular pins and custom pin images, allowing you to provide varied pin colors without needing multiple graphic assets. Reviewed By: fredliu Differential Revision: D2685581 fb-gh-sync-id: c7cf0af5c90fd8d1e9b3fec4b89206440b47ba8f
This commit is contained in:
committed by
facebook-github-bot-5
parent
63ef826d44
commit
5b5b55027b
@@ -397,6 +397,9 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[
|
||||
|
||||
+ (UIColor *)UIColor:(id)json
|
||||
{
|
||||
if (!json) {
|
||||
return nil;
|
||||
}
|
||||
if ([json isKindOfClass:[NSArray class]]) {
|
||||
NSArray *components = [self NSNumberArray:json];
|
||||
CGFloat alpha = components.count > 3 ? [self CGFloat:components[3]] : 1.0;
|
||||
@@ -404,13 +407,16 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[
|
||||
green:[self CGFloat:components[1]]
|
||||
blue:[self CGFloat:components[2]]
|
||||
alpha:alpha];
|
||||
} else {
|
||||
} else if ([json isKindOfClass:[NSNumber class]]) {
|
||||
NSUInteger argb = [self NSUInteger:json];
|
||||
CGFloat a = ((argb >> 24) & 0xFF) / 255.0;
|
||||
CGFloat r = ((argb >> 16) & 0xFF) / 255.0;
|
||||
CGFloat g = ((argb >> 8) & 0xFF) / 255.0;
|
||||
CGFloat b = (argb & 0xFF) / 255.0;
|
||||
return [UIColor colorWithRed:r green:g blue:b alpha:a];
|
||||
} else {
|
||||
RCTLogConvertError(json, @"a color");
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -460,12 +466,9 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[
|
||||
NSURL *URL = [self NSURL:path];
|
||||
NSString *scheme = URL.scheme.lowercaseString;
|
||||
if ([scheme isEqualToString:@"file"]) {
|
||||
if (RCTIsXCAssetURL(URL)) {
|
||||
// Image may reside inside a .car file, in which case we have no choice
|
||||
// but to use +[UIImage imageNamed] - but this method isn't thread safe
|
||||
NSString *assetName = RCTBundlePathForURL(URL);
|
||||
image = [UIImage imageNamed:assetName];
|
||||
} else {
|
||||
NSString *assetName = RCTBundlePathForURL(URL);
|
||||
image = [UIImage imageNamed:assetName];
|
||||
if (!image) {
|
||||
// Attempt to load from the file system
|
||||
NSString *filePath = URL.path;
|
||||
if (filePath.pathExtension.length == 0) {
|
||||
|
||||
@@ -84,6 +84,9 @@ RCT_EXTERN NSError *RCTErrorWithMessage(NSString *message);
|
||||
RCT_EXTERN id RCTNilIfNull(id value);
|
||||
RCT_EXTERN id RCTNullIfNil(id value);
|
||||
|
||||
// Convert NaN or infinite values to zero, as these aren't JSON-safe
|
||||
RCT_EXTERN double RCTZeroIfNaN(double value);
|
||||
|
||||
// Convert data to a Base64-encoded data URL
|
||||
RCT_EXTERN NSURL *RCTDataURL(NSString *mimeType, NSData *data);
|
||||
|
||||
@@ -96,3 +99,6 @@ RCT_EXTERN NSString *RCTBundlePathForURL(NSURL *URL);
|
||||
|
||||
// Determines if a given image URL actually refers to an XCAsset
|
||||
RCT_EXTERN BOOL RCTIsXCAssetURL(NSURL *imageURL);
|
||||
|
||||
// Converts a CGColor to a hex string
|
||||
RCT_EXTERN NSString *RCTColorToHexString(CGColorRef color);
|
||||
|
||||
@@ -420,6 +420,11 @@ id RCTNilIfNull(id value)
|
||||
return value == (id)kCFNull ? nil : value;
|
||||
}
|
||||
|
||||
RCT_EXTERN double RCTZeroIfNaN(double value)
|
||||
{
|
||||
return isnan(value) || isinf(value) ? 0 : value;
|
||||
}
|
||||
|
||||
NSURL *RCTDataURL(NSString *mimeType, NSData *data)
|
||||
{
|
||||
return [NSURL URLWithString:
|
||||
@@ -511,3 +516,62 @@ BOOL RCTIsXCAssetURL(NSURL *imageURL)
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
static void RCTGetRGBAColorComponents(CGColorRef color, CGFloat rgba[4])
|
||||
{
|
||||
CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(color));
|
||||
const CGFloat *components = CGColorGetComponents(color);
|
||||
switch (model)
|
||||
{
|
||||
case kCGColorSpaceModelMonochrome:
|
||||
{
|
||||
rgba[0] = components[0];
|
||||
rgba[1] = components[0];
|
||||
rgba[2] = components[0];
|
||||
rgba[3] = components[1];
|
||||
break;
|
||||
}
|
||||
case kCGColorSpaceModelRGB:
|
||||
{
|
||||
rgba[0] = components[0];
|
||||
rgba[1] = components[1];
|
||||
rgba[2] = components[2];
|
||||
rgba[3] = components[3];
|
||||
break;
|
||||
}
|
||||
case kCGColorSpaceModelCMYK:
|
||||
case kCGColorSpaceModelDeviceN:
|
||||
case kCGColorSpaceModelIndexed:
|
||||
case kCGColorSpaceModelLab:
|
||||
case kCGColorSpaceModelPattern:
|
||||
case kCGColorSpaceModelUnknown:
|
||||
{
|
||||
|
||||
#ifdef RCT_DEBUG
|
||||
//unsupported format
|
||||
RCTLogError(@"Unsupported color model: %i", model);
|
||||
#endif
|
||||
|
||||
rgba[0] = 0.0;
|
||||
rgba[1] = 0.0;
|
||||
rgba[2] = 0.0;
|
||||
rgba[3] = 1.0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NSString *RCTColorToHexString(CGColorRef color)
|
||||
{
|
||||
CGFloat rgba[4];
|
||||
RCTGetRGBAColorComponents(color, rgba);
|
||||
uint8_t r = rgba[0]*255;
|
||||
uint8_t g = rgba[1]*255;
|
||||
uint8_t b = rgba[2]*255;
|
||||
uint8_t a = rgba[3]*255;
|
||||
if (a < 255) {
|
||||
return [NSString stringWithFormat:@"#%02x%02x%02x%02x", r, g, b, a];
|
||||
} else {
|
||||
return [NSString stringWithFormat:@"#%02x%02x%02x", r, g, b];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user