-
-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios,v10): add new Image component, to generate mapbox images usi…
…ng react-native
- Loading branch information
Showing
13 changed files
with
360 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!-- This file was autogenerated from Image.tsx do not modify --> | ||
# <MapboxGL.Image /> | ||
|
||
|
||
## props | ||
| Prop | Type | Default | Required | Description | | ||
| ---- | :-- | :----- | :------ | :---------- | | ||
| name | `string` | `none` | `true` | Image name | | ||
| sdf | `boolean` | `none` | `false` | Make image an sdf optional - see [SDF icons](https://docs.mapbox.com/help/troubleshooting/using-recolorable-images-in-mapbox-maps/) | | ||
| stretchX | `Array` | `none` | `false` | stretch along x axis - optional | | ||
| stretchY | `Array` | `none` | `false` | stretch along y axis - optional | | ||
| children | `ReactElement` | `none` | `true` | Single react native view generating the image | | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import MapboxMaps | ||
|
||
class RCTMGLImage : UIView { | ||
@objc | ||
var name: String = "" | ||
|
||
var image: UIImage? = nil | ||
|
||
var sdf: Bool? = nil | ||
var stretchX: [[NSNumber]] = [] | ||
var stretchY: [[NSNumber]] = [] | ||
|
||
weak var images: RCTMGLImageSetter? = nil { | ||
didSet { | ||
DispatchQueue.main.async { self.setImage() } | ||
} | ||
} | ||
weak var bridge : RCTBridge! = nil | ||
|
||
var reactSubviews : [UIView] = [] | ||
|
||
// MARK: - subview management | ||
|
||
@objc open override func insertReactSubview(_ subview: UIView!, at atIndex: Int) { | ||
reactSubviews.insert(subview, at: atIndex) | ||
if reactSubviews.count > 1 { | ||
Logger.log(level: .error, message: "Image supports max 1 subview") | ||
} | ||
if image == nil { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + .microseconds(10)) { | ||
self.setImage() | ||
} | ||
} | ||
} | ||
|
||
@objc | ||
open override func removeReactSubview(_ subview: UIView!) { | ||
reactSubviews.removeAll(where: { $0 == subview }) | ||
} | ||
|
||
// MARK: - view shnapshot | ||
|
||
func changeImage(_ image: UIImage, name: String) { | ||
if let images = images { | ||
let _ = images.addImage(name: name, image: image, sdf: sdf, stretchX:stretchX, stretchY:stretchY, log: "RCTMGLImage.addImage") | ||
} | ||
} | ||
|
||
func setImage() { | ||
if let image = _createViewSnapshot() { | ||
changeImage(image, name: name) | ||
} | ||
} | ||
|
||
func _createViewSnapshot() -> UIImage? { | ||
let useDummyImage = false | ||
if useDummyImage { | ||
let size = CGSize(width: 32, height: 32) | ||
let renderer = UIGraphicsImageRenderer(size: size) | ||
let image = renderer.image { context in | ||
UIColor.darkGray.setStroke() | ||
context.stroke(CGRect(x: 0, y:0, width: 32, height: 32)) | ||
UIColor(red: 158/255, green: 215/255, blue: 245/255, alpha: 1).setFill() | ||
context.fill(CGRect(x: 2, y: 2, width: 30, height: 30)) | ||
} | ||
return image | ||
} | ||
guard reactSubviews.count > 0 else { | ||
return nil | ||
} | ||
return _createViewSnapshot(view: reactSubviews[0]) | ||
} | ||
|
||
func _createViewSnapshot(view: UIView) -> UIImage? { | ||
guard view.bounds.size.width > 0 && view.bounds.size.height > 0 else { | ||
return nil | ||
} | ||
|
||
let roundUp = 4 | ||
|
||
let adjustedSize = CGSize( | ||
width: ((Int(view.bounds.size.width)+roundUp-1)/roundUp)*roundUp, | ||
height: ((Int(view.bounds.size.height)+roundUp-1)/roundUp)*roundUp | ||
) | ||
|
||
let renderer = UIGraphicsImageRenderer(size: adjustedSize) | ||
let image = renderer.image { context in | ||
view.layer.render(in: context.cgContext) | ||
} | ||
return image | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#import <React/RCTBridgeModule.h> | ||
#import <React/RCTViewManager.h> | ||
|
||
@interface RCT_EXTERN_MODULE(RCTMGLImageManager, RCTViewManager) | ||
|
||
RCT_EXPORT_VIEW_PROPERTY(name, NSString) | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
@objc(RCTMGLImageManager) | ||
class RCTMGLImageManager : RCTViewManager { | ||
@objc | ||
override static func requiresMainQueueSetup() -> Bool { | ||
return true | ||
} | ||
|
||
override func view() -> UIView! { | ||
let layer = RCTMGLImage() | ||
layer.bridge = self.bridge | ||
return layer | ||
} | ||
} |
Oops, something went wrong.