Tap image attachment preview to view full screen.

// FREEBIE
pull/1/head
Matthew Chen 7 years ago
parent 722fc4d7a7
commit fb3bb852ca

@ -5,10 +5,12 @@
#import "AppSettingsViewController.h" #import "AppSettingsViewController.h"
#import "AttachmentSharing.h" #import "AttachmentSharing.h"
#import "ContactTableViewCell.h" #import "ContactTableViewCell.h"
#import "ConversationViewItem.h"
#import "DateUtil.h" #import "DateUtil.h"
#import "DebugUIPage.h" #import "DebugUIPage.h"
#import "Environment.h" #import "Environment.h"
#import "FingerprintViewController.h" #import "FingerprintViewController.h"
#import "FullImageViewController.h"
#import "HomeViewController.h" #import "HomeViewController.h"
#import "NSString+OWS.h" #import "NSString+OWS.h"
#import "NotificationsManager.h" #import "NotificationsManager.h"

@ -1946,8 +1946,9 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
UIWindow *window = [UIApplication sharedApplication].keyWindow; UIWindow *window = [UIApplication sharedApplication].keyWindow;
CGRect convertedRect = [imageView convertRect:imageView.bounds toView:window]; CGRect convertedRect = [imageView convertRect:imageView.bounds toView:window];
FullImageViewController *vc = FullImageViewController *vc = [[FullImageViewController alloc] initWithAttachmentStream:attachmentStream
[[FullImageViewController alloc] initWithAttachment:attachmentStream fromRect:convertedRect viewItem:viewItem]; fromRect:convertedRect
viewItem:viewItem];
[vc presentFromViewController:self]; [vc presentFromViewController:self];
} }

@ -7,13 +7,17 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@class ConversationViewItem; @class ConversationViewItem;
@class SignalAttachment;
@class TSAttachmentStream; @class TSAttachmentStream;
@interface FullImageViewController : OWSViewController @interface FullImageViewController : OWSViewController
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachmentStream // If viewItem is non-null, long press will show a menu controller.
fromRect:(CGRect)rect - (instancetype)initWithAttachmentStream:(TSAttachmentStream *)attachmentStream
viewItem:(ConversationViewItem *)viewItem; fromRect:(CGRect)rect
viewItem:(ConversationViewItem *_Nullable)viewItem;
- (instancetype)initWithAttachment:(SignalAttachment *)attachment fromRect:(CGRect)rect;
- (void)presentFromViewController:(UIViewController *)viewController; - (void)presentFromViewController:(UIViewController *)viewController;

@ -5,6 +5,7 @@
#import "FullImageViewController.h" #import "FullImageViewController.h"
#import "AttachmentSharing.h" #import "AttachmentSharing.h"
#import "ConversationViewItem.h" #import "ConversationViewItem.h"
#import "Signal-Swift.h"
#import "TSAttachmentStream.h" #import "TSAttachmentStream.h"
#import "TSInteraction.h" #import "TSInteraction.h"
#import "UIColor+OWS.h" #import "UIColor+OWS.h"
@ -56,8 +57,9 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) BOOL isPresenting; @property (nonatomic) BOOL isPresenting;
@property (nonatomic) NSData *fileData; @property (nonatomic) NSData *fileData;
@property (nonatomic) TSAttachmentStream *attachmentStream; @property (nonatomic, nullable) TSAttachmentStream *attachmentStream;
@property (nonatomic) ConversationViewItem *viewItem; @property (nonatomic, nullable) SignalAttachment *attachment;
@property (nonatomic, nullable) ConversationViewItem *viewItem;
@property (nonatomic) UIToolbar *footerBar; @property (nonatomic) UIToolbar *footerBar;
@ -65,10 +67,9 @@ NS_ASSUME_NONNULL_BEGIN
@implementation FullImageViewController @implementation FullImageViewController
- (instancetype)initWithAttachmentStream:(TSAttachmentStream *)attachmentStream
- (instancetype)initWithAttachment:(TSAttachmentStream *)attachmentStream fromRect:(CGRect)rect
fromRect:(CGRect)rect viewItem:(ConversationViewItem *_Nullable)viewItem
viewItem:(ConversationViewItem *)viewItem
{ {
self = [super initWithNibName:nil bundle:nil]; self = [super initWithNibName:nil bundle:nil];
@ -77,14 +78,65 @@ NS_ASSUME_NONNULL_BEGIN
self.attachmentStream = attachmentStream; self.attachmentStream = attachmentStream;
self.originRect = rect; self.originRect = rect;
self.viewItem = viewItem; self.viewItem = viewItem;
self.fileData = [NSData dataWithContentsOfURL:[attachmentStream mediaURL]];
} }
return self; return self;
} }
- (instancetype)initWithAttachment:(SignalAttachment *)attachment fromRect:(CGRect)rect
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.attachment = attachment;
self.originRect = rect;
}
return self;
}
- (NSURL *_Nullable)attachmentUrl
{
if (self.attachmentStream) {
return self.attachmentStream.mediaURL;
} else if (self.attachment) {
return self.attachment.dataUrl;
} else {
return nil;
}
}
- (NSData *)fileData
{
if (_fileData) {
NSURL *_Nullable url = self.attachmentUrl;
if (url) {
_fileData = [NSData dataWithContentsOfURL:url];
}
}
return _fileData;
}
- (UIImage *)image { - (UIImage *)image {
return self.attachmentStream.image; if (self.attachmentStream) {
return self.attachmentStream.image;
} else if (self.attachment) {
return self.attachment.image;
} else {
return nil;
}
}
- (BOOL)isAnimated
{
if (self.attachmentStream) {
return self.attachmentStream.isAnimated;
} else if (self.attachment) {
return self.attachment.isAnimatedImage;
} else {
return NO;
}
} }
- (void)loadView { - (void)loadView {
@ -150,7 +202,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)shareWasPressed:(id)sender { - (void)shareWasPressed:(id)sender {
DDLogInfo(@"%@: sharing image.", self.tag); DDLogInfo(@"%@: sharing image.", self.tag);
[AttachmentSharing showShareUIForURL:[self.attachmentStream mediaURL]]; [AttachmentSharing showShareUIForURL:self.attachmentUrl];
} }
- (void)initializeScrollView { - (void)initializeScrollView {
@ -162,13 +214,6 @@ NS_ASSUME_NONNULL_BEGIN
[self.contentView addSubview:self.scrollView]; [self.contentView addSubview:self.scrollView];
} }
- (BOOL)isAnimated
{
OWSAssert(self.attachmentStream);
return self.attachmentStream.isAnimated;
}
- (void)initializeImageView { - (void)initializeImageView {
if (self.isAnimated) { if (self.isAnimated) {
if ([self.fileData ows_isValidImage]) { if ([self.fileData ows_isValidImage]) {
@ -249,6 +294,9 @@ NS_ASSUME_NONNULL_BEGIN
- (void)longPressGesture:(UIGestureRecognizer *)sender { - (void)longPressGesture:(UIGestureRecognizer *)sender {
// We "eagerly" respond when the long press begins, not when it ends. // We "eagerly" respond when the long press begins, not when it ends.
if (sender.state == UIGestureRecognizerStateBegan) { if (sender.state == UIGestureRecognizerStateBegan) {
if (!self.viewItem) {
return;
}
[self.view becomeFirstResponder]; [self.view becomeFirstResponder];

@ -181,6 +181,9 @@ class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate {
let aspectRatio = image.size.width / image.size.height let aspectRatio = image.size.width / image.size.height
addSubviewWithScaleAspectFitLayout(view:animatedImageView, aspectRatio:aspectRatio) addSubviewWithScaleAspectFitLayout(view:animatedImageView, aspectRatio:aspectRatio)
contentView = animatedImageView contentView = animatedImageView
animatedImageView.isUserInteractionEnabled = true
animatedImageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(imageTapped)))
} }
private func addSubviewWithScaleAspectFitLayout(view: UIView, aspectRatio: CGFloat) { private func addSubviewWithScaleAspectFitLayout(view: UIView, aspectRatio: CGFloat) {
@ -212,7 +215,10 @@ class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate {
let aspectRatio = image.size.width / image.size.height let aspectRatio = image.size.width / image.size.height
addSubviewWithScaleAspectFitLayout(view:imageView, aspectRatio:aspectRatio) addSubviewWithScaleAspectFitLayout(view:imageView, aspectRatio:aspectRatio)
contentView = imageView contentView = imageView
}
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(imageTapped)))
}
private func createVideoPreview() { private func createVideoPreview() {
guard let image = attachment.videoPreview() else { guard let image = attachment.videoPreview() else {
@ -391,13 +397,44 @@ class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate {
audioPlayButton?.imageView?.tintColor = UIColor.ows_materialBlue() audioPlayButton?.imageView?.tintColor = UIColor.ows_materialBlue()
} }
// MARK: - Full Screen Image
func imageTapped(sender: UIGestureRecognizer) {
guard sender.state == .recognized else {
return
}
guard let fromView = sender.view else {
return
}
guard let fromViewController = fromViewController() else {
return
}
let window = UIApplication.shared.keyWindow
let convertedRect = fromView.convert(fromView.bounds, to:window)
let viewController = FullImageViewController(attachment:attachment, from:convertedRect)
viewController.present(from:fromViewController)
}
private func fromViewController() -> UIViewController? {
var responder: UIResponder? = self
while true {
if responder == nil {
return nil
}
if let viewController = responder as? UIViewController {
return viewController
}
responder = responder?.next
}
}
// MARK: - Video Playback // MARK: - Video Playback
func videoTapped(sender: UIGestureRecognizer) { func videoTapped(sender: UIGestureRecognizer) {
guard let dataUrl = attachment.dataUrl else { guard sender.state == .recognized else {
return return
} }
guard sender.state == .recognized else { guard let dataUrl = attachment.dataUrl else {
return return
} }
guard let videoPlayer = MPMoviePlayerController(contentURL: dataUrl) else { guard let videoPlayer = MPMoviePlayerController(contentURL: dataUrl) else {

Loading…
Cancel
Save