From cc2e062b85588ada389cec196db78d6c60c55b2c Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 7 Feb 2019 08:42:50 -0700 Subject: [PATCH] CR: clean up graphics context code --- .../CropScaleImageViewController.swift | 13 ++++++++----- .../Views/ImageEditor/ImageEditorModel.swift | 2 +- SignalMessaging/utils/OWSContactAvatarBuilder.m | 15 +++++++++------ SignalMessaging/utils/UIImage+OWS.swift | 3 ++- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Signal/src/ViewControllers/CropScaleImageViewController.swift b/Signal/src/ViewControllers/CropScaleImageViewController.swift index 4ba967e14..5e11d4525 100644 --- a/Signal/src/ViewControllers/CropScaleImageViewController.swift +++ b/Signal/src/ViewControllers/CropScaleImageViewController.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// Copyright (c) 2019 Open Whisper Systems. All rights reserved. // import Foundation @@ -490,15 +490,18 @@ import SignalMessaging let dstScale: CGFloat = 1.0 // The size is specified in pixels, not in points. UIGraphicsBeginImageContextWithOptions(dstSizePixels, !hasAlpha, dstScale) - let context = UIGraphicsGetCurrentContext() - context!.interpolationQuality = .high + guard let context = UIGraphicsGetCurrentContext() else { + owsFailDebug("could not generate dst image.") + return nil + } + context.interpolationQuality = .high let imageViewFrame = imageRenderRect(forDstSize: dstSizePixels) srcImage.draw(in: imageViewFrame) - let scaledImage = UIGraphicsGetImageFromCurrentImageContext() - if scaledImage == nil { + guard let scaledImage = UIGraphicsGetImageFromCurrentImageContext() else { owsFailDebug("could not generate dst image.") + return nil } UIGraphicsEndImageContext() return scaledImage diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift b/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift index ba5b6b280..246930422 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift @@ -648,7 +648,7 @@ public class ImageEditorModel: NSObject { defer { UIGraphicsEndImageContext() } guard let context = UIGraphicsGetCurrentContext() else { - owsFailDebug("Could not create output context.") + owsFailDebug("context was unexpectedly nil") return nil } context.interpolationQuality = .high diff --git a/SignalMessaging/utils/OWSContactAvatarBuilder.m b/SignalMessaging/utils/OWSContactAvatarBuilder.m index 3ae39c075..beedd12fc 100644 --- a/SignalMessaging/utils/OWSContactAvatarBuilder.m +++ b/SignalMessaging/utils/OWSContactAvatarBuilder.m @@ -193,9 +193,11 @@ NS_ASSUME_NONNULL_BEGIN CGFloat paddedheight = baseImage.size.height * paddingFactor; UIGraphicsBeginImageContextWithOptions(CGSizeMake(paddedWidth, paddedheight), NO, 0.0); - CGContextRef context = UIGraphicsGetCurrentContext(); - UIGraphicsPushContext(context); - + CGContextRef _Nullable context = UIGraphicsGetCurrentContext(); + if (context == nil) { + OWSFailDebug(@"failure: context was unexpectedly nil"); + return nil; + } [backgroundColor setFill]; CGContextFillRect(context, CGRectMake(0, 0, paddedWidth, paddedheight)); @@ -203,10 +205,11 @@ NS_ASSUME_NONNULL_BEGIN (paddedheight - baseImage.size.height) / 2.0f); [baseImage drawAtPoint:origin]; - - // Clean up and get the new image. - UIGraphicsPopContext(); UIImage *paddedImage = UIGraphicsGetImageFromCurrentImageContext(); + if (paddedImage == nil) { + OWSFailDebug(@"failure: paddedImage was unexpectedly nil"); + return nil; + } UIGraphicsEndImageContext(); return paddedImage; diff --git a/SignalMessaging/utils/UIImage+OWS.swift b/SignalMessaging/utils/UIImage+OWS.swift index 360543f88..6ad19b9d3 100644 --- a/SignalMessaging/utils/UIImage+OWS.swift +++ b/SignalMessaging/utils/UIImage+OWS.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// Copyright (c) 2019 Open Whisper Systems. All rights reserved. // import Foundation @@ -11,6 +11,7 @@ extension UIImage { defer { UIGraphicsEndImageContext() } guard let context = UIGraphicsGetCurrentContext() else { + owsFailDebug("context was unexpectedly nil") return nil }