CR: clean up graphics context code

pull/1/head
Michael Kirk 6 years ago
parent 2323cc21f0
commit cc2e062b85

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -490,15 +490,18 @@ import SignalMessaging
let dstScale: CGFloat = 1.0 // The size is specified in pixels, not in points. let dstScale: CGFloat = 1.0 // The size is specified in pixels, not in points.
UIGraphicsBeginImageContextWithOptions(dstSizePixels, !hasAlpha, dstScale) UIGraphicsBeginImageContextWithOptions(dstSizePixels, !hasAlpha, dstScale)
let context = UIGraphicsGetCurrentContext() guard let context = UIGraphicsGetCurrentContext() else {
context!.interpolationQuality = .high owsFailDebug("could not generate dst image.")
return nil
}
context.interpolationQuality = .high
let imageViewFrame = imageRenderRect(forDstSize: dstSizePixels) let imageViewFrame = imageRenderRect(forDstSize: dstSizePixels)
srcImage.draw(in: imageViewFrame) srcImage.draw(in: imageViewFrame)
let scaledImage = UIGraphicsGetImageFromCurrentImageContext() guard let scaledImage = UIGraphicsGetImageFromCurrentImageContext() else {
if scaledImage == nil {
owsFailDebug("could not generate dst image.") owsFailDebug("could not generate dst image.")
return nil
} }
UIGraphicsEndImageContext() UIGraphicsEndImageContext()
return scaledImage return scaledImage

@ -648,7 +648,7 @@ public class ImageEditorModel: NSObject {
defer { UIGraphicsEndImageContext() } defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext() else { guard let context = UIGraphicsGetCurrentContext() else {
owsFailDebug("Could not create output context.") owsFailDebug("context was unexpectedly nil")
return nil return nil
} }
context.interpolationQuality = .high context.interpolationQuality = .high

@ -193,9 +193,11 @@ NS_ASSUME_NONNULL_BEGIN
CGFloat paddedheight = baseImage.size.height * paddingFactor; CGFloat paddedheight = baseImage.size.height * paddingFactor;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(paddedWidth, paddedheight), NO, 0.0); UIGraphicsBeginImageContextWithOptions(CGSizeMake(paddedWidth, paddedheight), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextRef _Nullable context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context); if (context == nil) {
OWSFailDebug(@"failure: context was unexpectedly nil");
return nil;
}
[backgroundColor setFill]; [backgroundColor setFill];
CGContextFillRect(context, CGRectMake(0, 0, paddedWidth, paddedheight)); CGContextFillRect(context, CGRectMake(0, 0, paddedWidth, paddedheight));
@ -203,10 +205,11 @@ NS_ASSUME_NONNULL_BEGIN
(paddedheight - baseImage.size.height) / 2.0f); (paddedheight - baseImage.size.height) / 2.0f);
[baseImage drawAtPoint:origin]; [baseImage drawAtPoint:origin];
// Clean up and get the new image.
UIGraphicsPopContext();
UIImage *paddedImage = UIGraphicsGetImageFromCurrentImageContext(); UIImage *paddedImage = UIGraphicsGetImageFromCurrentImageContext();
if (paddedImage == nil) {
OWSFailDebug(@"failure: paddedImage was unexpectedly nil");
return nil;
}
UIGraphicsEndImageContext(); UIGraphicsEndImageContext();
return paddedImage; return paddedImage;

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -11,6 +11,7 @@ extension UIImage {
defer { UIGraphicsEndImageContext() } defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext() else { guard let context = UIGraphicsGetCurrentContext() else {
owsFailDebug("context was unexpectedly nil")
return nil return nil
} }

Loading…
Cancel
Save