Respond to CR.

pull/2/head
Matthew Chen 6 years ago
parent 2f00cbdfeb
commit 73b36c5400

@ -941,7 +941,7 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD
if let imageEditorModel = attachmentItem.imageEditorModel,
let imageMediaView = mediaMessageView.contentView {
let imageEditorView = ImageEditorView(model: imageEditorModel)
let imageEditorView = ImageEditorView(model: imageEditorModel, delegate: self)
if imageEditorView.configureSubviews() {
mediaMessageView.isHidden = true
@ -1319,6 +1319,19 @@ extension AttachmentPrepViewController: UIScrollViewDelegate {
// MARK: -
extension AttachmentPrepViewController: ImageEditorViewDelegate {
public func imageEditor(presentFullScreenOverlay viewController: UIViewController) {
let navigationController = OWSNavigationController(rootViewController: viewController)
navigationController.modalPresentationStyle = .overFullScreen
self.present(navigationController, animated: true) {
// Do nothing.
}
}
}
// MARK: -
class BottomToolView: UIView {
let mediaMessageTextToolbar: MediaMessageTextToolbar
let galleryRailView: GalleryRailView
@ -1338,7 +1351,7 @@ class BottomToolView: UIView {
super.init(frame: .zero)
// Specifying autorsizing mask and an intrinsic content size allows proper
// Specifying auto-resizing mask and an intrinsic content size allows proper
// sizing when used as an input accessory view.
self.autoresizingMask = .flexibleHeight
self.translatesAutoresizingMaskIntoConstraints = false

@ -144,8 +144,6 @@ public class ImageEditorStrokeItem: ImageEditorItem {
@objc
public class ImageEditorTextItem: ImageEditorItem {
// Until we need to serialize these items,
// just use UIColor.
@objc
public let color: UIColor
@ -188,7 +186,7 @@ public class ImageEditorTextItem: ImageEditorItem {
public init(color: UIColor,
font: UIFont,
text: String,
unitCenter: ImageEditorSample = CGPoint(x: 0.5, y: 0.5),
unitCenter: ImageEditorSample = ImageEditorSample(x: 0.5, y: 0.5),
unitWidth: CGFloat = ImageEditorTextItem.kDefaultUnitWidth,
rotationRadians: CGFloat = 0.0,
scaling: CGFloat = 1.0) {

@ -17,7 +17,7 @@ public struct ImageEditorPinchState {
self.angleRadians = angleRadians
}
static func empty() -> ImageEditorPinchState {
static var empty: ImageEditorPinchState {
return ImageEditorPinchState(centroid: .zero, distance: 1.0, angleRadians: 0)
}
}
@ -29,9 +29,9 @@ public struct ImageEditorPinchState {
// than UIPinchGestureRecognizer.
public class ImageEditorPinchGestureRecognizer: UIGestureRecognizer {
public var pinchStateStart = ImageEditorPinchState.empty()
public var pinchStateStart = ImageEditorPinchState.empty
public var pinchStateLast = ImageEditorPinchState.empty()
public var pinchStateLast = ImageEditorPinchState.empty
// MARK: - Touch Handling

@ -153,7 +153,7 @@ class ImageEditorTextViewController: OWSViewController, VAlignTextViewDelegate {
self.view.addSubview(textView)
textView.autoPinTopToSuperviewMargin()
textView.autoHCenterInSuperview()
// In order to having text wrapping be as WYSIWYG as possible, we limit the text view
// In order to have text wrapping be as WYSIWYG as possible, we limit the text view
// to the max text width on the image.
let maxTextWidthPoints = max(self.maxTextWidthPoints, 200)
textView.autoSetDimension(.width, toSize: maxTextWidthPoints, relation: .lessThanOrEqual)

@ -4,35 +4,6 @@
import UIKit
extension UIView {
public func renderAsImage() -> UIImage? {
return renderAsImage(opaque: false, scale: UIScreen.main.scale)
}
public func renderAsImage(opaque: Bool, scale: CGFloat) -> UIImage? {
if #available(iOS 10, *) {
let format = UIGraphicsImageRendererFormat()
format.scale = scale
format.opaque = opaque
let renderer = UIGraphicsImageRenderer(bounds: self.bounds,
format: format)
return renderer.image { (context) in
self.layer.render(in: context.cgContext)
}
} else {
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, scale)
if let _ = UIGraphicsGetCurrentContext() {
drawHierarchy(in: bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
owsFailDebug("Could not create graphics context.")
return nil
}
}
}
private class EditorTextLayer: CATextLayer {
let itemId: String
@ -50,11 +21,20 @@ private class EditorTextLayer: CATextLayer {
// MARK: -
@objc
public protocol ImageEditorViewDelegate: class {
func imageEditor(presentFullScreenOverlay viewController: UIViewController)
}
// MARK: -
// A view for editing outgoing image attachments.
// It can also be used to render the final output.
@objc
public class ImageEditorView: UIView, ImageEditorModelDelegate, ImageEditorTextViewControllerDelegate, UIGestureRecognizerDelegate {
weak var delegate: ImageEditorViewDelegate?
private let model: ImageEditorModel
enum EditorMode: String {
@ -76,8 +56,9 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate, ImageEditorTextV
private var currentColor = ImageEditorView.defaultColor
@objc
public required init(model: ImageEditorModel) {
public required init(model: ImageEditorModel, delegate: ImageEditorViewDelegate) {
self.model = model
self.delegate = delegate
super.init(frame: .zero)
@ -393,21 +374,12 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate, ImageEditorTextV
toggle(editorMode: .none)
guard let viewController = self.containingViewController() else {
owsFailDebug("Can't find view controller.")
return
}
isEditingTextItem = true
let maxTextWidthPoints = imageView.width() * ImageEditorTextItem.kDefaultUnitWidth
let textEditor = ImageEditorTextViewController(delegate: self, textItem: textItem, maxTextWidthPoints: maxTextWidthPoints)
let navigationController = OWSNavigationController(rootViewController: textEditor)
navigationController.modalPresentationStyle = .overFullScreen
viewController.present(navigationController, animated: true) {
// Do nothing.
}
self.delegate?.imageEditor(presentFullScreenOverlay: textEditor)
}
// MARK: - Pinch Gesture
@ -985,7 +957,8 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate, ImageEditorTextV
// I don't think we need to enable allowsFontSubpixelQuantization
// or set truncationMode.
// This text needs to be rendered at a scale that reflects the scaling.
// This text needs to be rendered at a scale that reflects the sceen scaling
// AND the item's scaling.
layer.contentsScale = UIScreen.main.scale * item.scaling
// TODO: Min with measured width.

@ -117,8 +117,6 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value);
+ (UIView *)verticalStackWithSubviews:(NSArray<UIView *> *)subviews spacing:(int)spacing;
- (nullable UIViewController *)containingViewController;
#pragma mark - Debugging
- (void)addBorderWithColor:(UIColor *)color;

@ -448,19 +448,6 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value)
return container;
}
- (nullable UIViewController *)containingViewController
{
UIResponder *responder = self;
while (responder) {
if ([responder isKindOfClass:[UIViewController class]]) {
UIViewController *viewController = (UIViewController *)responder;
return viewController;
}
responder = responder.nextResponder;
}
return nil;
}
#pragma mark - Debugging
- (void)addBorderWithColor:(UIColor *)color

@ -44,3 +44,32 @@ public extension UINavigationController {
CATransaction.commit()
}
}
extension UIView {
public func renderAsImage() -> UIImage? {
return renderAsImage(opaque: false, scale: UIScreen.main.scale)
}
public func renderAsImage(opaque: Bool, scale: CGFloat) -> UIImage? {
if #available(iOS 10, *) {
let format = UIGraphicsImageRendererFormat()
format.scale = scale
format.opaque = opaque
let renderer = UIGraphicsImageRenderer(bounds: self.bounds,
format: format)
return renderer.image { (context) in
self.layer.render(in: context.cgContext)
}
} else {
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, scale)
if let _ = UIGraphicsGetCurrentContext() {
drawHierarchy(in: bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
owsFailDebug("Could not create graphics context.")
return nil
}
}
}

@ -5,27 +5,12 @@
import Foundation
extension UIImage {
private func image(with view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext() else {
owsFailDebug("context was unexpectedly nil")
return nil
}
view.layer.render(in: context)
let image = UIGraphicsGetImageFromCurrentImageContext()
return image
}
@objc
public func asTintedImage(color: UIColor) -> UIImage? {
let template = self.withRenderingMode(.alwaysTemplate)
let imageView = UIImageView(image: template)
imageView.tintColor = color
return image(with: imageView)
return imageView.renderAsImage(opaque: imageView.isOpaque, scale: UIScreen.main.scale)
}
}

Loading…
Cancel
Save