Removed placeholder image logic

Summary: To prevent layout popping, when inserting images inside text we would render a blank placeholder image while the real image was loading. It turns out that this isn't necessary, as we can just specify the size of the image without having an actual image to display.

Reviewed By: javache

Differential Revision: D3212766

fb-gh-sync-id: e98851b32a2d0ae809fc0a4be47e6b77f3b17996
fbshipit-source-id: e98851b32a2d0ae809fc0a4be47e6b77f3b17996
This commit is contained in:
Nick Lockwood 2016-04-25 03:30:54 -07:00 committed by Facebook Github Bot 2
parent 0bf737ff0a
commit 37f4ec6e16
5 changed files with 4 additions and 110 deletions

View File

@ -133,52 +133,4 @@ RCTAssertEqualSizes(a.size, b.size); \
RCTAssertEqualRects(expected, result);
}
- (void)testPlaceholderImage
{
CGSize size = {45, 22};
CGFloat expectedScale = 1.0;
UIImage *image = RCTGetPlaceholderImage(size, nil);
RCTAssertEqualSizes(size, image.size);
XCTAssertEqual(expectedScale, image.scale);
}
- (void)testPlaceholderNonintegralSize
{
CGSize size = {3.0/2, 7.0/3};
CGFloat expectedScale = 6;
CGSize pixelSize = {
round(size.width * expectedScale),
round(size.height * expectedScale)
};
UIImage *image = RCTGetPlaceholderImage(size, nil);
RCTAssertEqualSizes(size, image.size);
XCTAssertEqual(pixelSize.width, CGImageGetWidth(image.CGImage));
XCTAssertEqual(pixelSize.height, CGImageGetHeight(image.CGImage));
XCTAssertEqual(expectedScale, image.scale);
}
- (void)testPlaceholderSquareImage
{
CGSize size = {333, 333};
CGFloat expectedScale = 1.0/333;
CGSize pixelSize = {1, 1};
UIImage *image = RCTGetPlaceholderImage(size, nil);
RCTAssertEqualSizes(size, image.size);
XCTAssertEqual(pixelSize.width, CGImageGetWidth(image.CGImage));
XCTAssertEqual(pixelSize.height, CGImageGetHeight(image.CGImage));
XCTAssertEqual(expectedScale, image.scale);
}
- (void)testPlaceholderNonsquareImage
{
CGSize size = {640, 480};
CGFloat expectedScale = 1.0/160;
CGSize pixelSize = {4, 3};
UIImage *image = RCTGetPlaceholderImage(size, nil);
RCTAssertEqualSizes(size, image.size);
XCTAssertEqual(pixelSize.width, CGImageGetWidth(image.CGImage));
XCTAssertEqual(pixelSize.height, CGImageGetHeight(image.CGImage));
XCTAssertEqual(expectedScale, image.scale);
}
@end

View File

@ -93,11 +93,4 @@ RCT_EXTERN UIImage *__nullable RCTTransformImage(UIImage *image,
*/
RCT_EXTERN BOOL RCTImageHasAlpha(CGImageRef image);
/**
* Create a solid placeholder image of the specified size and color to display
* while loading an image. If color is not specified, image will be transparent.
*/
RCT_EXTERN UIImage *__nullable RCTGetPlaceholderImage(CGSize size,
UIColor *__nullable color);
NS_ASSUME_NONNULL_END

View File

@ -16,8 +16,6 @@
#import "RCTLog.h"
#import "RCTUtils.h"
static const CGFloat RCTThresholdValue = 0.0001;
static CGFloat RCTCeilValue(CGFloat value, CGFloat scale)
{
return ceil(value * scale) / scale;
@ -337,46 +335,3 @@ BOOL RCTImageHasAlpha(CGImageRef image)
return YES;
}
}
UIImage *__nullable RCTGetPlaceholderImage(CGSize size,
UIColor *__nullable color)
{
if (size.width <= 0 || size.height <= 0) {
return nil;
}
// If dimensions are nonintegral, increase scale
CGFloat scale = 1;
if (size.width - floor(size.width) > RCTThresholdValue) {
scale *= round(1.0 / (size.width - floor(size.width)));
}
if (size.height - floor(size.height) > RCTThresholdValue) {
scale *= round(1.0 / (size.height - floor(size.height)));
}
// Use Euclid's algorithm to find the greatest common divisor
// between the specified placeholder width and height;
NSInteger a = size.width * scale;
NSInteger b = size.height * scale;
while (a != 0) {
NSInteger c = a;
a = b % a;
b = c;
}
// Divide the placeholder image scale by the GCD we found above. This allows
// us to save memory by creating the smallest possible placeholder image
// with the correct aspect ratio, then scaling it up at display time.
scale /= b;
// Fill image with specified color
CGFloat alpha = CGColorGetAlpha(color.CGColor);
UIGraphicsBeginImageContextWithOptions(size, ABS(1.0 - alpha) < RCTThresholdValue, scale);
if (alpha > 0) {
[color setFill];
UIRectFill((CGRect){CGPointZero, size});
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

View File

@ -52,10 +52,6 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init)
RCTZeroIfNaN(self.height),
};
if (!_image) {
_image = RCTGetPlaceholderImage(imageSize, nil);
}
__weak RCTShadowVirtualImage *weakSelf = self;
_cancellationBlock = [_bridge.imageLoader loadImageWithTag:_source.imageURL.absoluteString
size:imageSize

View File

@ -217,12 +217,10 @@ static css_dim_t RCTMeasure(void *context, float width, css_measure_mode_t width
RCTShadowRawText *shadowRawText = (RCTShadowRawText *)child;
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:shadowRawText.text ?: @""]];
} else if ([child conformsToProtocol:@protocol(RCTImageComponent)]) {
UIImage *image = ((id<RCTImageComponent>)child).image;
if (image) {
NSTextAttachment *imageAttachment = [NSTextAttachment new];
imageAttachment.image = image;
[attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:imageAttachment]];
}
NSTextAttachment *imageAttachment = [NSTextAttachment new];
imageAttachment.image = ((id<RCTImageComponent>)child).image;
imageAttachment.bounds = (CGRect){CGPointZero, {RCTZeroIfNaN(child.width), RCTZeroIfNaN(child.height)}};
[attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:imageAttachment]];
} else {
RCTLogError(@"<Text> can't have any children except <Text>, <Image> or raw strings");
}