Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
Use smaller snapshot on ios
Browse files Browse the repository at this point in the history
Summary:
We are sending enormous snapshots, sometimes > 10mb.

By using a small image it saves quite a bit.

In future i would like to revisit jpeg as it will compress more, but that will require protocol changes

Reviewed By: lblasa

Differential Revision: D56755341

fbshipit-source-id: b96d55b17154842b3d8db20dcac0aa67ff8c57ff
  • Loading branch information
Luke De Feo authored and facebook-github-bot committed May 1, 2024
1 parent 7cd9e2e commit cfa7206
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,32 @@
FB_LINKABLE(UIImage_Foundation)
@implementation UIImage (Foundation)

- (UIImage*)resizeImage:(UIImage*)image withRatio:(CGFloat)ratio {
CGSize newSize =
CGSizeMake(image.size.width * ratio, image.size.height * ratio);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

- (NSString*)toFoundation {
uint64_t t0 = UIDPerformanceNow();
NSData* data = UIImagePNGRepresentation(self);

UIImage* resized = [self resizeImage:self withRatio:0.5];
uint64_t t1 = UIDPerformanceNow();
UIDPerformanceAggregate(
@"snapshotResizeMs", UIDMonotonicTimeConvertMachUnitsToMS(t1 - t0));

NSData* data = UIImagePNGRepresentation(resized);
NSString* base64 = [data
base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
uint64_t t1 = UIDPerformanceNow();
uint64_t t2 = UIDPerformanceNow();

UIDPerformanceAggregate(
@"snapshotSerialisationMS",
UIDMonotonicTimeConvertMachUnitsToMS(t1 - t0));
UIDMonotonicTimeConvertMachUnitsToMS(t2 - t1));

UIDPerformanceAggregate(@"snapshotSizeKB", base64.length / 1000);

Expand Down

0 comments on commit cfa7206

Please sign in to comment.