Add image bytes property of UIImage (#22731)

Summary:
* Added a `reactDecodedImageBytes` property of `UIImage`, to give the chances if user wants to do custom memory calculation.
* Fixes wrong calculation of GIF decoded image bytes.

Changelog:
----------

[iOS] [Fixed] - Fixes image decoded bytes calculation
Pull Request resolved: https://github.com/facebook/react-native/pull/22731

Differential Revision: D13817094

Pulled By: cpojer

fbshipit-source-id: ddc793d4734cba4e36f53b634bd3655883922c19
This commit is contained in:
zhongwuzw 2019-01-25 02:42:15 -08:00 committed by Facebook Github Bot
parent 5218932b13
commit 103880b3c6
3 changed files with 31 additions and 1 deletions

View File

@ -71,7 +71,7 @@ static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat sc
if (!image) {
return;
}
CGFloat bytes = image.size.width * image.size.height * image.scale * image.scale * 4;
NSInteger bytes = image.reactDecodedImageBytes;
if (bytes <= RCTMaxCachableDecodedImageSizeInBytes) {
[self->_decodedImageCache setObject:image
forKey:cacheKey

View File

@ -52,6 +52,11 @@ typedef dispatch_block_t RCTImageLoaderCancellationBlock;
@property (nonatomic, copy) CAKeyframeAnimation *reactKeyframeAnimation;
/**
* Image's memory bytes. It has the dafault calculation of single image of GIF, if you have custom calculation of image decoded bytes, you can assign it using your value.
*/
@property (nonatomic, assign) NSInteger reactDecodedImageBytes;
@end
@interface RCTImageLoader : NSObject <RCTBridgeModule, RCTURLRequestHandler>

View File

@ -20,6 +20,17 @@
#import "RCTImageCache.h"
#import "RCTImageUtils.h"
static NSInteger RCTImageBytesForImage(UIImage *image)
{
CAKeyframeAnimation *keyFrameAnimation = [image reactKeyframeAnimation];
NSInteger singleImageBytes = image.size.width * image.size.height * image.scale * image.scale * 4;
if (keyFrameAnimation) {
return keyFrameAnimation.values.count * singleImageBytes;
} else {
return image.images ? image.images.count * singleImageBytes : singleImageBytes;
}
}
@implementation UIImage (React)
- (CAKeyframeAnimation *)reactKeyframeAnimation
@ -32,6 +43,20 @@
objc_setAssociatedObject(self, @selector(reactKeyframeAnimation), reactKeyframeAnimation, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSInteger)reactDecodedImageBytes
{
NSNumber *imageBytes = objc_getAssociatedObject(self, _cmd);
if (!imageBytes) {
imageBytes = @(RCTImageBytesForImage(self));
}
return [imageBytes integerValue];
}
- (void)setReactDecodedImageBytes:(NSInteger)bytes
{
objc_setAssociatedObject(self, @selector(reactDecodedImageBytes), @(bytes), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@implementation RCTImageLoader