Skip to content

Commit 114d33d

Browse files
committed
add iOS support
1 parent 028d62b commit 114d33d

File tree

8 files changed

+378
-16
lines changed

8 files changed

+378
-16
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ https://github.com/invertase/react-native-firebase
1616

1717
For iOS add the following pod to your podfile:
1818
```
19-
pod 'FirebaseUI/Storage'
19+
pod 'SDWebImage', '~> 4.0'
2020
```
2121
and run pod install.
2222

@@ -75,6 +75,7 @@ export class MyFirebaseImageView extends Component<void, void, void> {
7575
<ImageView
7676
{...imageProps}
7777
path='firebase/storage/path'
78+
timestamp={0} //optional, can be used to specify last modified time for same storage path
7879
resizeMode='cover' //'cover', 'contain', 'stretch'
7980
/>
8081
)
@@ -94,6 +95,7 @@ export class MyFirebasePhotoView extends Component<void, void, void> {
9495
<PhotoView
9596
{...imageProps}
9697
path='firebase/storage/path'
98+
timestamp={0} //optional, can be used to specify last modified time for same storage path
9799
resizeMode='cover' //'cover', 'contain', 'stretch'
98100
/>
99101
)

ios/FirebaseUIImageView.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
//
88

99
#import "Firebase.h"
10-
#import <FirebaseStorageUI/FirebaseStorageUI.h>
1110

1211
#import <React/RCTResizeMode.h>
1312

1413
@interface FirebaseImageView : UIImageView
1514

1615
@property (nonatomic, copy) NSString *path;
16+
@property (nonatomic, strong) NSNumber *timestamp;
1717
@property (nonatomic, assign) RCTResizeMode resizeMode;
1818

1919
@end
20-

ios/FirebaseUIImageView.m

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,28 @@
99

1010
#import "FirebaseUIImageView.h"
1111
#import "Firebase.h"
12-
#import <FirebaseStorageUI/FirebaseStorageUI.h>
12+
#import "UIImageView+FirebaseStorage.h"
1313

1414
@implementation FirebaseImageView
1515

1616
- (void)setPath:(NSString *)path
1717
{
1818
_path = path;
19-
20-
// Reference to an image file in Firebase Storage
21-
FIRStorageReference *reference = [[FIRStorage storage] referenceWithPath:_path];
22-
23-
// Placeholder image
24-
UIImage *placeholderImage;
25-
26-
// Load the image using SDWebImage
27-
[self sd_setImageWithStorageReference:reference placeholderImage:placeholderImage];
19+
20+
[self reloadImage];
21+
}
22+
23+
-(void)setTimestamp:(NSNumber*)timestamp
24+
{
25+
_timestamp = timestamp;
26+
[self reloadImage];
2827
}
2928

3029
- (void)setResizeMode:(RCTResizeMode)resizeMode
3130
{
3231
if (_resizeMode != resizeMode) {
3332
_resizeMode = resizeMode;
34-
33+
3534
if (_resizeMode == RCTResizeModeRepeat) {
3635
// Repeat resize mode is handled by the UIImage. Use scale to fill
3736
// so the repeated image fills the UIImageView.
@@ -44,4 +43,14 @@ - (void)setResizeMode:(RCTResizeMode)resizeMode
4443
}
4544
}
4645

46+
-(void)reloadImage
47+
{
48+
// Reference to an image file in Firebase Storage
49+
FIRStorageReference *reference = [[FIRStorage storage] referenceWithPath:_path];
50+
51+
// Load the image using SDWebImage
52+
NSString* timestamp = [_timestamp stringValue];
53+
[self sd_setImageWithStorageReference:reference customKey:timestamp];
54+
}
55+
4756
@end

ios/RCTFirebaseImageViewManager.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ @implementation RCTFirebaseImageViewManager
1414

1515
RCT_EXPORT_MODULE()
1616
RCT_EXPORT_VIEW_PROPERTY(path, NSString)
17+
RCT_EXPORT_VIEW_PROPERTY(timestamp, NSNumber)
1718
RCT_EXPORT_VIEW_PROPERTY(resizeMode, RCTResizeMode)
1819

20+
1921
- (UIView *)view
2022
{
2123
FirebaseImageView *imageView = [[FirebaseImageView alloc] init];
22-
24+
2325
return imageView;
2426
}
2527

ios/RNFirebaseUi.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
B3E7B58A1CC2AC0600A0062D /* RCTFirebaseImageViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RCTFirebaseImageViewManager.m */; };
1111
BA04DF251F1506A800122A3C /* FirebaseUIImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = BA04DF241F1506A800122A3C /* FirebaseUIImageView.m */; };
12+
BAC95E541FA3D2C400E33F4D /* UIImageView+FirebaseStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = BAC95E511FA3D2C300E33F4D /* UIImageView+FirebaseStorage.m */; };
1213
/* End PBXBuildFile section */
1314

1415
/* Begin PBXCopyFilesBuildPhase section */
@@ -29,6 +30,8 @@
2930
BA04DF241F1506A800122A3C /* FirebaseUIImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirebaseUIImageView.m; sourceTree = "<group>"; };
3031
BA04DF261F1506CE00122A3C /* FirebaseUIImageView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FirebaseUIImageView.h; sourceTree = "<group>"; };
3132
BA43A3101F14BB0300791965 /* RCTFirebaseImageViewManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTFirebaseImageViewManager.h; sourceTree = "<group>"; };
33+
BAC95E511FA3D2C300E33F4D /* UIImageView+FirebaseStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+FirebaseStorage.m"; sourceTree = "<group>"; };
34+
BAC95E531FA3D2C300E33F4D /* UIImageView+FirebaseStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+FirebaseStorage.h"; sourceTree = "<group>"; };
3235
/* End PBXFileReference section */
3336

3437
/* Begin PBXFrameworksBuildPhase section */
@@ -53,6 +56,8 @@
5356
58B511D21A9E6C8500147676 = {
5457
isa = PBXGroup;
5558
children = (
59+
BAC95E531FA3D2C300E33F4D /* UIImageView+FirebaseStorage.h */,
60+
BAC95E511FA3D2C300E33F4D /* UIImageView+FirebaseStorage.m */,
5661
BA04DF261F1506CE00122A3C /* FirebaseUIImageView.h */,
5762
BA04DF241F1506A800122A3C /* FirebaseUIImageView.m */,
5863
B3E7B5891CC2AC0600A0062D /* RCTFirebaseImageViewManager.m */,
@@ -119,6 +124,7 @@
119124
files = (
120125
B3E7B58A1CC2AC0600A0062D /* RCTFirebaseImageViewManager.m in Sources */,
121126
BA04DF251F1506A800122A3C /* FirebaseUIImageView.m in Sources */,
127+
BAC95E541FA3D2C400E33F4D /* UIImageView+FirebaseStorage.m in Sources */,
122128
);
123129
runOnlyForDeploymentPostprocessing = 0;
124130
};

ios/UIImageView+FirebaseStorage.h

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@import UIKit;
18+
19+
#import <FirebaseStorage/FirebaseStorage.h>
20+
#import <SDWebImage/UIImageView+WebCache.h>
21+
#import <SDWebImage/UIImage+MultiFormat.h>
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
@interface UIImageView (FirebaseStorage)
26+
27+
/**
28+
* Returns the maximum image download size, in bytes. Defaults to 10e6.
29+
*/
30+
+ (UInt64)sd_defaultMaxImageSize;
31+
32+
/**
33+
* Sets the maximum image download size, in bytes.
34+
* @param size The new maximum image download size.
35+
*/
36+
+ (void)sd_setDefaultMaxImageSize:(UInt64)size;
37+
38+
/**
39+
* The current download task, if the image view is downloading an image.
40+
* Must be invoked on the main queue.
41+
*/
42+
@property (nonatomic, readonly, nullable) FIRStorageDownloadTask *sd_currentDownloadTask;
43+
44+
/**
45+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
46+
*
47+
* @param storageRef A Firebase Storage reference containing an image.
48+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
49+
* could not be found in cache).
50+
*/
51+
- (nullable FIRStorageDownloadTask *)sd_setImageWithStorageReference:(FIRStorageReference *)storageRef;
52+
53+
/**
54+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
55+
* Must be invoked on the main queue.
56+
*
57+
* @param storageRef A Firebase Storage reference containing an image.
58+
* @param customKey An custom caching key to append to the storage path.
59+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
60+
* could not be found in cache).
61+
*/
62+
- (nullable FIRStorageDownloadTask *)sd_setImageWithStorageReference:(FIRStorageReference *)storageRef
63+
customKey:(NSString*)customKey;
64+
65+
/**
66+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
67+
* Must be invoked on the main queue.
68+
*
69+
* @param storageRef A Firebase Storage reference containing an image.
70+
* @param placeholder An image to display while the download is in progress.
71+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
72+
* could not be found in cache).
73+
*/
74+
- (nullable FIRStorageDownloadTask *)sd_setImageWithStorageReference:(FIRStorageReference *)storageRef
75+
placeholderImage:(nullable UIImage *)placeholder;
76+
77+
/**
78+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
79+
* Must be invoked on the main queue.
80+
*
81+
* @param storageRef A Firebase Storage reference containing an image.
82+
* @param placeholder An image to display while the download is in progress.
83+
* @param completion A closure to handle events when the image finishes downloading.
84+
* The closure is not guaranteed to be invoked on the main thread.
85+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
86+
* could not be found in cache).
87+
*/
88+
- (nullable FIRStorageDownloadTask *)sd_setImageWithStorageReference:(FIRStorageReference *)storageRef
89+
placeholderImage:(nullable UIImage *)placeholder
90+
completion:(void (^_Nullable)(UIImage *_Nullable,
91+
NSError *_Nullable,
92+
SDImageCacheType,
93+
FIRStorageReference *))completion;
94+
95+
/**
96+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
97+
* Must be invoked on the main queue.
98+
*
99+
* @param storageRef A Firebase Storage reference containing an image.
100+
* @param size The maximum size of the downloaded image. If the downloaded image
101+
* exceeds this size, an error will be raised in the completion block.
102+
* @param placeholder An image to display while the download is in progress.
103+
* @param completion A closure to handle events when the image finishes downloading.
104+
* The closure is not guaranteed to be invoked on the main thread.
105+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
106+
* could not be found in cache).
107+
*/
108+
- (nullable FIRStorageDownloadTask *)sd_setImageWithStorageReference:(FIRStorageReference *)storageRef
109+
maxImageSize:(UInt64)size
110+
placeholderImage:(nullable UIImage *)placeholder
111+
completion:(void (^_Nullable)(UIImage *_Nullable,
112+
NSError *_Nullable,
113+
SDImageCacheType,
114+
FIRStorageReference *))completion;
115+
116+
/**
117+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
118+
* Must be invoked on the main queue.
119+
*
120+
* @param storageRef A Firebase Storage reference containing an image.
121+
* @param size The maximum size of the downloaded image. If the downloaded image
122+
* exceeds this size, an error will be raised in the completion block.
123+
* @param placeholder An image to display while the download is in progress.
124+
* @param cache An image cache to check for images before downloading.
125+
* @param completion A closure to handle events when the image finishes downloading.
126+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
127+
* could not be found in cache).
128+
*/
129+
- (nullable FIRStorageDownloadTask *)sd_setImageWithStorageReference:(FIRStorageReference *)storageRef
130+
maxImageSize:(UInt64)size
131+
placeholderImage:(nullable UIImage *)placeholder
132+
cache:(nullable SDImageCache *)cache
133+
completion:(void (^_Nullable)(UIImage *_Nullable,
134+
NSError *_Nullable,
135+
SDImageCacheType,
136+
FIRStorageReference *))completion;
137+
138+
/**
139+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
140+
* Must be invoked on the main queue.
141+
*
142+
* @param storageRef A Firebase Storage reference containing an image.
143+
* @param size The maximum size of the downloaded image. If the downloaded image
144+
* exceeds this size, an error will be raised in the completion block.
145+
* @param placeholder An image to display while the download is in progress.
146+
* @param cache An image cache to check for images before downloading.
147+
* @param completion A closure to handle events when the image finishes downloading.
148+
* @param customKey An custom caching key to append to the storage path.
149+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
150+
* could not be found in cache).
151+
*/
152+
- (nullable FIRStorageDownloadTask *)sd_setImageWithStorageReference:(FIRStorageReference *)storageRef
153+
maxImageSize:(UInt64)size
154+
placeholderImage:(nullable UIImage *)placeholder
155+
cache:(nullable SDImageCache *)cache
156+
completion:(void (^_Nullable)(UIImage *_Nullable,
157+
NSError *_Nullable,
158+
SDImageCacheType,
159+
FIRStorageReference *))completion
160+
customKey:(NSString*)customKey;
161+
162+
@end
163+
164+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)