mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-04 15:44:24 +00:00
Load Image from URL
This commit is contained in:
@@ -32,6 +32,7 @@ import com.facebook.imagepipeline.request.ImageRequestBuilder;
|
|||||||
import com.facebook.react.bridge.ReadableMap;
|
import com.facebook.react.bridge.ReadableMap;
|
||||||
import com.facebook.react.common.ReactConstants;
|
import com.facebook.react.common.ReactConstants;
|
||||||
import com.facebook.react.uimanager.annotations.ReactProp;
|
import com.facebook.react.uimanager.annotations.ReactProp;
|
||||||
|
import com.facebook.react.views.imagehelper.ImageSource;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
@@ -47,7 +48,7 @@ public class ImageShadowNode extends RenderableShadowNode {
|
|||||||
private String mY;
|
private String mY;
|
||||||
private String mW;
|
private String mW;
|
||||||
private String mH;
|
private String mH;
|
||||||
private Uri mUri;
|
private String uriString;
|
||||||
private float mImageRatio;
|
private float mImageRatio;
|
||||||
private String mAlign;
|
private String mAlign;
|
||||||
private int mMeetOrSlice;
|
private int mMeetOrSlice;
|
||||||
@@ -80,7 +81,7 @@ public class ImageShadowNode extends RenderableShadowNode {
|
|||||||
@ReactProp(name = "src")
|
@ReactProp(name = "src")
|
||||||
public void setSrc(@Nullable ReadableMap src) {
|
public void setSrc(@Nullable ReadableMap src) {
|
||||||
if (src != null) {
|
if (src != null) {
|
||||||
String uriString = src.getString("uri");
|
uriString = src.getString("uri");
|
||||||
|
|
||||||
if (uriString == null || uriString.isEmpty()) {
|
if (uriString == null || uriString.isEmpty()) {
|
||||||
//TODO: give warning about this
|
//TODO: give warning about this
|
||||||
@@ -92,7 +93,6 @@ public class ImageShadowNode extends RenderableShadowNode {
|
|||||||
} else {
|
} else {
|
||||||
mImageRatio = 0f;
|
mImageRatio = 0f;
|
||||||
}
|
}
|
||||||
mUri = Uri.parse(uriString);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +112,9 @@ public class ImageShadowNode extends RenderableShadowNode {
|
|||||||
@Override
|
@Override
|
||||||
public void draw(final Canvas canvas, final Paint paint, final float opacity) {
|
public void draw(final Canvas canvas, final Paint paint, final float opacity) {
|
||||||
if (!mLoading.get()) {
|
if (!mLoading.get()) {
|
||||||
final ImageRequest request = ImageRequestBuilder.newBuilderWithSource(mUri).build();
|
final ImageSource imageSource = new ImageSource(getThemedContext(), uriString);
|
||||||
|
|
||||||
|
final ImageRequest request = ImageRequestBuilder.newBuilderWithSource(imageSource.getUri()).build();
|
||||||
if (Fresco.getImagePipeline().isInBitmapMemoryCache(request)) {
|
if (Fresco.getImagePipeline().isInBitmapMemoryCache(request)) {
|
||||||
tryRender(request, canvas, paint, opacity * mOpacity);
|
tryRender(request, canvas, paint, opacity * mOpacity);
|
||||||
} else {
|
} else {
|
||||||
@@ -137,7 +139,9 @@ public class ImageShadowNode extends RenderableShadowNode {
|
|||||||
public void onNewResultImpl(Bitmap bitmap) {
|
public void onNewResultImpl(Bitmap bitmap) {
|
||||||
mLoading.set(false);
|
mLoading.set(false);
|
||||||
SvgViewShadowNode shadowNode = getSvgShadowNode();
|
SvgViewShadowNode shadowNode = getSvgShadowNode();
|
||||||
shadowNode.markUpdated();
|
if(shadowNode != null) {
|
||||||
|
shadowNode.markUpdated();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -8,11 +8,13 @@
|
|||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
|
#import <React/RCTBridge.h>
|
||||||
#import "RNSVGRenderable.h"
|
#import "RNSVGRenderable.h"
|
||||||
#import "RNSVGVBMOS.h"
|
#import "RNSVGVBMOS.h"
|
||||||
|
|
||||||
@interface RNSVGImage : RNSVGRenderable
|
@interface RNSVGImage : RNSVGRenderable
|
||||||
|
|
||||||
|
@property (nonatomic, weak) RCTBridge *bridge;
|
||||||
@property (nonatomic, assign) id src;
|
@property (nonatomic, assign) id src;
|
||||||
@property (nonatomic, strong) NSString* x;
|
@property (nonatomic, strong) NSString* x;
|
||||||
@property (nonatomic, strong) NSString* y;
|
@property (nonatomic, strong) NSString* y;
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
#import "RNSVGImage.h"
|
#import "RNSVGImage.h"
|
||||||
#import "RCTConvert+RNSVG.h"
|
#import "RCTConvert+RNSVG.h"
|
||||||
#import <React/RCTImageSource.h>
|
#import <React/RCTImageSource.h>
|
||||||
|
#import <React/RCTImageView.h>
|
||||||
|
#import <React/RCTImageLoader.h>
|
||||||
|
#import <React/RCTImageViewManager.h>
|
||||||
#import <React/RCTLog.h>
|
#import <React/RCTLog.h>
|
||||||
#import "RNSVGViewBox.h"
|
#import "RNSVGViewBox.h"
|
||||||
|
|
||||||
@@ -16,6 +19,7 @@
|
|||||||
{
|
{
|
||||||
CGImageRef _image;
|
CGImageRef _image;
|
||||||
CGFloat _imageRatio;
|
CGFloat _imageRatio;
|
||||||
|
RCTImageLoaderCancellationBlock _reloadImageCancellationBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setSrc:(id)src
|
- (void)setSrc:(id)src
|
||||||
@@ -32,8 +36,18 @@
|
|||||||
_imageRatio = 0.0;
|
_imageRatio = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
_image = CGImageRetain([RCTConvert CGImage:src]);
|
RCTImageLoaderCancellationBlock previousCancellationBlock = _reloadImageCancellationBlock;
|
||||||
[self invalidate];
|
if (previousCancellationBlock) {
|
||||||
|
previousCancellationBlock();
|
||||||
|
_reloadImageCancellationBlock = nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
_reloadImageCancellationBlock = [self.bridge.imageLoader loadImageWithURLRequest:[RCTConvert NSURLRequest:src] callback:^(NSError *error, UIImage *image) {
|
||||||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
_image = CGImageRetain(image.CGImage);
|
||||||
|
[self invalidate];
|
||||||
|
});
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setX:(NSString *)x
|
- (void)setX:(NSString *)x
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ RCT_EXPORT_MODULE()
|
|||||||
|
|
||||||
- (RNSVGRenderable *)node
|
- (RNSVGRenderable *)node
|
||||||
{
|
{
|
||||||
return [RNSVGImage new];
|
RNSVGImage *svgImage = [RNSVGImage new];
|
||||||
|
svgImage.bridge = self.bridge;
|
||||||
|
|
||||||
|
return svgImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
RCT_EXPORT_VIEW_PROPERTY(x, NSString)
|
RCT_EXPORT_VIEW_PROPERTY(x, NSString)
|
||||||
|
|||||||
Reference in New Issue
Block a user