Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Nov 12, 2023
1 parent 2cf17df commit 0f234f1
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion WebDriverAgentLib/Utilities/FBImageProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ - (nullable NSData*)processedJpegImageWithData:(NSData *)imageData
: size;

CGImageRef resultImage = NULL;
if (orientation != kCGImagePropertyOrientationUp || usesScaling) {
if (orientation != kCGImagePropertyOrientationUp) {
resultImage = CGImageSourceCreateImageAtIndex(imageDataRef, 0, NULL);
CGImageRef originalImage = resultImage;
size_t bitsPerComponent = CGImageGetBitsPerComponent(originalImage);
Expand Down Expand Up @@ -138,9 +138,21 @@ - (nullable NSData*)processedJpegImageWithData:(NSData *)imageData
resultImage = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
CGImageRelease(originalImage);
} else if (usesScaling) {
CGFloat scaledMaxPixelSize = MAX(size.width, size.height) * scalingFactor;
CFDictionaryRef params = (__bridge CFDictionaryRef)@{
(const NSString *)kCGImageSourceCreateThumbnailWithTransform: @(YES),
(const NSString *)kCGImageSourceCreateThumbnailFromImageIfAbsent: @(YES),
(const NSString *)kCGImageSourceThumbnailMaxPixelSize: @(scaledMaxPixelSize)
};
resultImage = CGImageSourceCreateThumbnailAtIndex(imageDataRef, 0, params);
}
CFRelease(imageDataRef);
if (NULL == resultImage) {
if (orientation != kCGImagePropertyOrientationUp || usesScaling) {
// This is suboptimal, but better to have something than nothing at all
// NSLog(@"The image cannot be preprocessed. Passing it as is");
}
// No scaling and/or orientation fixing was neecessary
return imageData;
}
Expand Down

0 comments on commit 0f234f1

Please sign in to comment.