Skip to content

Commit

Permalink
Merged Iragne's facebook#1406
Browse files Browse the repository at this point in the history
  • Loading branch information
PublicParadise committed Jun 17, 2015
2 parents b8eaeca + 5584659 commit cdd022e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions Examples/UIExplorer/CameraRollExample.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var CameraRollExample = React.createClass({
return (
<View key={asset} style={styles.row}>
<Image
assetThumbnail={true}
source={asset.node.image}
style={imageStyle}
/>
Expand Down
13 changes: 12 additions & 1 deletion Libraries/Image/Image.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ var Image = React.createClass({
source: PropTypes.shape({
uri: PropTypes.string,
}),
/**
* Force display of the ALAsset in thumbnail format
* This property reduce the memory size and only work with ALAsset
*/
assetThumbnail: PropTypes.bool,
/**
* A static image to display while downloading the final image off the
* network.
Expand Down Expand Up @@ -154,7 +159,11 @@ var Image = React.createClass({
tintColor: style.tintColor,
});
if (isStored) {
nativeProps.imageTag = source.uri;
if (this.props.assetThumbnail === true){
nativeProps.assetThumbnail = source.uri;
}else{
nativeProps.imageTag = source.uri;
}
} else {
nativeProps.src = source.uri;
}
Expand All @@ -179,6 +188,8 @@ var nativeOnlyProps = {
defaultImageSrc: true,
imageTag: true,
resizeMode: true,
contentMode: true,
assetThumb:true,
};
if (__DEV__) {
verifyPropTypes(Image, RCTStaticImage.viewConfig, nativeOnlyProps);
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/RCTCameraRollManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ @implementation RCTCameraRollManager
successCallback:(RCTResponseSenderBlock)successCallback
errorCallback:(RCTResponseSenderBlock)errorCallback)
{
[RCTImageLoader loadImageWithTag:imageTag callback:^(NSError *loadError, UIImage *loadedImage) {
[RCTImageLoader loadImageWithTag:imageTag thumb:NO callback:^(NSError *loadError, UIImage *loadedImage) {
if (loadError) {
errorCallback(@[[loadError localizedDescription]]);
return;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/RCTImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
* Will always call callback on main thread.
*/
+ (void)loadImageWithTag:(NSString *)tag
thumb:(BOOL)thumb
callback:(void (^)(NSError *error, id /* UIImage or CAAnimation */ image))callback;

@end
11 changes: 9 additions & 2 deletions Libraries/Image/RCTImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ + (ALAssetsLibrary *)assetsLibrary
* Can be called from any thread.
* Will always call callback on main thread.
*/
+ (void)loadImageWithTag:(NSString *)imageTag callback:(void (^)(NSError *error, id image))callback
+ (void)loadImageWithTag:(NSString *)imageTag thumb:(BOOL)thumb callback:(void (^)(NSError *error, id image))callback
{
if ([imageTag hasPrefix:@"assets-library"]) {
[[RCTImageLoader assetsLibrary] assetForURL:[NSURL URLWithString:imageTag] resultBlock:^(ALAsset *asset) {
Expand All @@ -77,10 +77,17 @@ + (void)loadImageWithTag:(NSString *)imageTag callback:(void (^)(NSError *error,
dispatch_async(RCTImageLoaderQueue(), ^{
// Also make sure the image is released immediately after it's used so it
// doesn't spike the memory up during the process.

@autoreleasepool {
UIImage *image = nil;
ALAssetRepresentation *representation = [asset defaultRepresentation];
ALAssetOrientation orientation = [representation orientation];
UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0f orientation:(UIImageOrientation)orientation];
if (!thumb){
image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0f orientation:(UIImageOrientation)orientation];
}else{
image = [UIImage imageWithCGImage:[asset thumbnail] scale:1.0f orientation:(UIImageOrientation)orientation];
}

RCTDispatchCallbackOnMainQueue(callback, nil, image);
}
});
Expand Down
23 changes: 22 additions & 1 deletion Libraries/Image/RCTStaticImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ - (UIView *)view
RCT_CUSTOM_VIEW_PROPERTY(imageTag, NSString, RCTStaticImage)
{
if (json) {
[RCTImageLoader loadImageWithTag:[RCTConvert NSString:json] callback:^(NSError *error, id image) {
[RCTImageLoader loadImageWithTag:[RCTConvert NSString:json] thumb:NO callback:^(NSError *error, id image) {
if (error) {
RCTLogWarn(@"%@", error.localizedDescription);
}
Expand All @@ -70,5 +70,26 @@ - (UIView *)view
view.image = defaultView.image;
}
}
RCT_CUSTOM_VIEW_PROPERTY(assetThumbnail, NSString, RCTStaticImage)
{
if (json) {
[RCTImageLoader loadImageWithTag:[RCTConvert NSString:json] thumb:YES callback:^(NSError *error, id image) {
if (error) {
RCTLogWarn(@"%@", error.localizedDescription);
}
if ([image isKindOfClass:[CAAnimation class]]) {
[view.layer addAnimation:image forKey:@"contents"];
} else {
[view.layer removeAnimationForKey:@"contents"];
view.image = image;
}
}];
} else {
[view.layer removeAnimationForKey:@"contents"];
view.image = defaultView.image;
}
}



@end

0 comments on commit cdd022e

Please sign in to comment.