Skip to content

Commit

Permalink
Initial commit for facebook#930 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aroth committed May 1, 2015
1 parent f8a4467 commit 453064b
Show file tree
Hide file tree
Showing 8 changed files with 18,760 additions and 8 deletions.
1 change: 1 addition & 0 deletions Examples/UIExplorer/CameraRollExample.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var CameraRollExample = React.createClass({
<Image
source={asset.node.image}
style={imageStyle}
representation={'thumbnail'}
/>
<View style={styles.info}>
<Text style={styles.url}>{asset.node.image.uri}</Text>
Expand Down
1 change: 1 addition & 0 deletions Examples/UIExplorer/CameraRollView.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var CameraRollView = React.createClass({
return (
<Image
source={asset.node.image}
representation={'thumbnail'}
style={imageStyle}
/>
);
Expand Down
12 changes: 11 additions & 1 deletion Libraries/Image/Image.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ var Image = React.createClass({
source: PropTypes.shape({
uri: PropTypes.string,
}),

/**
* Static Image (ALAsset) representation
* full -> full resolution
* screen -> full screen
* thumbnail -> thumbnail
*/
representation: PropTypes.oneOf(['full', 'screen', 'thumbnail']),

/**
* A static image to display while downloading the final image off the
* network.
Expand Down Expand Up @@ -158,7 +167,8 @@ var Image = React.createClass({
tintColor: style.tintColor,
});
if (isStored) {
nativeProps.imageTag = source.uri;
// aroth: TODO
nativeProps.imageTag = { uri: source.uri, representation: this.props.representation };
} else {
nativeProps.src = source.uri;
}
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 representation:@"full" 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 @@ -15,6 +15,6 @@
@interface RCTImageLoader : NSObject

+ (ALAssetsLibrary *)assetsLibrary;
+ (void)loadImageWithTag:(NSString *)tag callback:(void (^)(NSError *error, UIImage *image))callback;
+ (void)loadImageWithTag:(NSString *)tag representation:(NSString *)rep callback:(void (^)(NSError *error, UIImage *image))callback;

@end
19 changes: 16 additions & 3 deletions Libraries/Image/RCTImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ + (ALAssetsLibrary *)assetsLibrary
return assetsLibrary;
}

+ (void)loadImageWithTag:(NSString *)imageTag callback:(void (^)(NSError *error, UIImage *image))callback
+ (void)loadImageWithTag:(NSString *)imageTag representation:(NSString *)rep callback:(void (^)(NSError *error, UIImage *image))callback
{
if ([imageTag hasPrefix:@"assets-library"]) {
[[RCTImageLoader assetsLibrary] assetForURL:[NSURL URLWithString:imageTag] resultBlock:^(ALAsset *asset) {
Expand All @@ -64,11 +64,24 @@ + (void)loadImageWithTag:(NSString *)imageTag callback:(void (^)(NSError *error,
// Also make sure the image is released immediately after it's used so it
// doesn't spike the memory up during the process.
@autoreleasepool {
// aroth: TODO
ALAssetRepresentation *representation = [asset defaultRepresentation];
ALAssetOrientation orientation = [representation orientation];
UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0f orientation:(UIImageOrientation)orientation];
callback(nil, image);
UIImage *image;

if( [rep isEqualToString:@"screen"] ){
image = [UIImage imageWithCGImage:[representation fullScreenImage] scale:1.0f orientation:(UIImageOrientation)orientation];
}else if( [rep isEqualToString:@"thumbnail"] ){
image = [UIImage imageWithCGImage:[asset thumbnail] scale:1.0f orientation:(UIImageOrientation)orientation];
}else{
image = [UIImage imageWithCGImage:[representation fullResolutionImage] scale:1.0f orientation:(UIImageOrientation)orientation];
}

dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, image);
});
}

});
} else {
NSString *errorText = [NSString stringWithFormat:@"Failed to load asset at URL %@ with no error message.", imageTag];
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Image/RCTStaticImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ - (UIView *)view
view.tintColor = defaultView.tintColor;
}
}
RCT_CUSTOM_VIEW_PROPERTY(imageTag, NSString, RCTStaticImage)
RCT_CUSTOM_VIEW_PROPERTY(imageTag, NSDictionary, RCTStaticImage)
{
if (json) {
[RCTImageLoader loadImageWithTag:[RCTConvert NSString:json] callback:^(NSError *error, UIImage *image) {
[RCTImageLoader loadImageWithTag:[RCTConvert NSString:json[@"uri"]] representation:[RCTConvert NSString:json[@"representation"]] callback:^(NSError *error, UIImage *image) {
if (error) {
RCTLogWarn(@"%@", error.localizedDescription);
}
Expand Down
Loading

0 comments on commit 453064b

Please sign in to comment.