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
@ -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

@ -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

@ -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;

@ -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
}

Loading…
Cancel
Save