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