Skip to content

Commit

Permalink
Adopt UIGraphicsImageRenderer API
Browse files Browse the repository at this point in the history
Summary:
Apple suggested to this new API on iOS 10+.

> // Any new bitmap drawing code is encouraged to use UIGraphicsImageRenderer in lieu of this API.

WWDC18 Reference: https://developer.apple.com/videos/play/wwdc2018/219/
> Use UIGraphicsImageRenderer to create and draw to an image buffer
Supports Wide Color, unlike UIGraphicsBeginImageContext()
Combine with UIImageView for efficient offscreen rendering

Per https://nshipster.com/image-resizing/#performance-benchmarks, the new API runs even faster than the C version, probably due to more smart context reuses/management.

Changelog:
[iOS][Changed] - Adopt UIGraphicsImageRenderer API

Reviewed By: philIip

Differential Revision: D35699584

fbshipit-source-id: 7a1e2109d5e121fb396c1014f4ed0a892211b0cc
  • Loading branch information
matrush authored and facebook-github-bot committed Apr 23, 2022
1 parent 7454044 commit d70d7fd
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 265 deletions.
13 changes: 8 additions & 5 deletions Libraries/Image/RCTImageBlurUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
if (CGImageGetBitsPerPixel(imageRef) != 32 ||
CGImageGetBitsPerComponent(imageRef) != 8 ||
!((CGImageGetBitmapInfo(imageRef) & kCGBitmapAlphaInfoMask))) {
UIGraphicsBeginImageContextWithOptions(inputImage.size, NO, inputImage.scale);
[inputImage drawAtPoint:CGPointZero];
imageRef = UIGraphicsGetImageFromCurrentImageContext().CGImage;
UIGraphicsEndImageContext();
UIGraphicsImageRendererFormat *const rendererFormat = [UIGraphicsImageRendererFormat defaultFormat];
rendererFormat.scale = inputImage.scale;
UIGraphicsImageRenderer *const renderer = [[UIGraphicsImageRenderer alloc] initWithSize:inputImage.size format:rendererFormat];

imageRef = [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull context) {
[inputImage drawAtPoint:CGPointZero];
}].CGImage;
}

vImage_Buffer buffer1, buffer2;
Expand All @@ -51,7 +54,7 @@

//create temp buffer
vImage_Error tempBufferSize = vImageBoxConvolve_ARGB8888(&buffer1, &buffer2, NULL, 0, 0, boxSize, boxSize,
NULL, kvImageGetTempBufferSize | kvImageEdgeExtend);
NULL, kvImageGetTempBufferSize | kvImageEdgeExtend);
if (tempBufferSize < 0) {
free(buffer1.data);
free(buffer2.data);
Expand Down
15 changes: 8 additions & 7 deletions Libraries/Image/RCTImageUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,14 @@ BOOL RCTUpscalingRequired(CGSize sourceSize, CGFloat sourceScale,
}

BOOL opaque = !RCTImageHasAlpha(image.CGImage);
UIGraphicsBeginImageContextWithOptions(destSize, opaque, destScale);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextConcatCTM(currentContext, transform);
[image drawAtPoint:CGPointZero];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
UIGraphicsImageRendererFormat *const rendererFormat = [UIGraphicsImageRendererFormat defaultFormat];
rendererFormat.opaque = opaque;
rendererFormat.scale = destScale;
UIGraphicsImageRenderer *const renderer = [[UIGraphicsImageRenderer alloc] initWithSize:destSize format:rendererFormat];
return [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull context) {
CGContextConcatCTM(context.CGContext, transform);
[image drawAtPoint:CGPointZero];
}];
}

BOOL RCTImageHasAlpha(CGImageRef image)
Expand Down
Loading

0 comments on commit d70d7fd

Please sign in to comment.