Add a preview from a picked picture

Can capture a video/picture
This commit is contained in:
ylecollen 2014-12-22 11:52:29 +01:00
parent 37c2a1411b
commit e74ce12ea5
3 changed files with 252 additions and 10 deletions

View file

@ -19,6 +19,8 @@
// Customize UIImageView in order to let UIImageView handle automatically remote url
@interface CustomImageView : UIImageView
typedef void (^blockCustomImageView_onClick)(CustomImageView *imageView, NSString* title);
@property (strong, nonatomic) NSString *placeholder;
@property (strong, nonatomic) NSString *imageURL;
@ -28,5 +30,11 @@
// Information about the media represented by this image (image, video...)
@property (strong, nonatomic) NSDictionary *mediaInfo;
// Let the user defines some custom buttons over the tabbar
- (void)setLeftButtonTitle :leftButtonTitle handler:(blockCustomImageView_onClick)handler;
- (void)setRightButtonTitle:rightButtonTitle handler:(blockCustomImageView_onClick)handler;
- (void)dismissSelection;
@end

View file

@ -16,15 +16,48 @@
#import "CustomImageView.h"
#import "MediaManager.h"
#import "AppDelegate.h"
@interface CustomImageView () {
id imageLoader;
UIActivityIndicatorView *loadingWheel;
// validation buttons
UIButton* leftButton;
UIButton* rightButton;
NSString* leftButtonTitle;
NSString* rightButtonTitle;
blockCustomImageView_onClick leftHandler;
blockCustomImageView_onClick rightHandler;
UIView* bottomBarView;
}
@end
@implementation CustomImageView
#define CUSTOM_IMAGE_VIEW_BUTTON_WIDTH 100
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
leftButtonTitle = nil;
leftHandler = nil;
rightButtonTitle = nil;
rightHandler = nil;
self.backgroundColor = [UIColor blackColor];
self.contentMode = UIViewContentModeScaleAspectFit;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
}
return self;
}
- (void)dealloc {
if (imageLoader) {
[MediaManager cancel:imageLoader];
@ -34,6 +67,10 @@
[loadingWheel removeFromSuperview];
loadingWheel = nil;
}
if (bottomBarView) {
[bottomBarView removeFromSuperview];
bottomBarView = nil;
}
}
- (void)startActivityIndicator {
@ -63,6 +100,85 @@
#pragma mark -
- (IBAction)onButtonToggle:(id)sender
{
if (sender == leftButton) {
dispatch_async(dispatch_get_main_queue(), ^{
leftHandler(self, leftButtonTitle);
});
} else if (sender == rightButton) {
dispatch_async(dispatch_get_main_queue(), ^{
rightHandler(self, rightButtonTitle);
});
}
}
// add a generic button to the bottom view
// return the added UIButton
- (UIButton*) addbuttonWithTitle:(NSString*)title {
UIButton* button = [[UIButton alloc] init];
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateHighlighted];
// default background text color
CGFloat textColorFactor = 146.0 / 255.0;
UIColor* textColor = [UIColor colorWithRed:textColorFactor green:textColorFactor blue:textColorFactor alpha:1.0];
[button setTitleColor:textColor forState:UIControlStateNormal];
[button setTitleColor:textColor forState:UIControlStateHighlighted];
// keep the bottomView background color
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:@selector(onButtonToggle:) forControlEvents:UIControlEventTouchUpInside];
[bottomBarView addSubview:button];
return button;
}
- (void)layoutSubviews {
// call upper layer
[super layoutSubviews];
// check if the dedicated buttons are already added
if (leftButtonTitle || rightButtonTitle) {
if (!bottomBarView) {
bottomBarView = [[UIView alloc] init];
if (leftButtonTitle) {
leftButton = [self addbuttonWithTitle:leftButtonTitle];
}
rightButton = [[UIButton alloc] init];
if (rightButtonTitle) {
rightButton = [self addbuttonWithTitle:rightButtonTitle];
}
// default tabbar background color
CGFloat base = 248.0 / 255.0f;
bottomBarView.backgroundColor = [UIColor colorWithRed:base green:base blue:base alpha:1.0];
[[AppDelegate theDelegate].masterTabBarController.tabBar addSubview:bottomBarView];
}
// manage the item
CGRect tabBarFrame = [AppDelegate theDelegate].masterTabBarController.tabBar.frame;
tabBarFrame.origin.y = 0;
bottomBarView.frame = tabBarFrame;
if (leftButton) {
leftButton.frame = CGRectMake(0, 0, CUSTOM_IMAGE_VIEW_BUTTON_WIDTH, bottomBarView.frame.size.height);
}
if (rightButton) {
rightButton.frame = CGRectMake(bottomBarView.frame.size.width - CUSTOM_IMAGE_VIEW_BUTTON_WIDTH, 0, CUSTOM_IMAGE_VIEW_BUTTON_WIDTH, bottomBarView.frame.size.height);
}
}
}
- (void)setHideActivityIndicator:(BOOL)hideActivityIndicator {
_hideActivityIndicator = hideActivityIndicator;
if (hideActivityIndicator) {
@ -106,4 +222,23 @@
}
}
#pragma mark - buttons management
- (void)setLeftButtonTitle: aLeftButtonTitle handler:(blockCustomImageView_onClick)handler {
leftButtonTitle = aLeftButtonTitle;
leftHandler = handler;
}
- (void)setRightButtonTitle:aRightButtonTitle handler:(blockCustomImageView_onClick)handler {
rightButtonTitle = aRightButtonTitle;
rightHandler = handler;
}
- (void)dismissSelection {
if (bottomBarView) {
[bottomBarView removeFromSuperview];
bottomBarView = nil;
}
}
@end

View file

@ -100,6 +100,7 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
@property (strong, nonatomic) MXRoom *mxRoom;
@property (strong, nonatomic) CustomAlert *actionMenu;
@property (strong, nonatomic) CustomImageView* imageValidationView;
@end
@implementation RoomViewController
@ -197,6 +198,7 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTextFieldChange:) name:UITextFieldTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
@ -218,9 +220,21 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
isKeyboardObserver = NO;
}
if (self.imageValidationView) {
[self.imageValidationView dismissSelection];
[self.imageValidationView removeFromSuperview];
self.imageValidationView = nil;
}
if (highResImage) {
[highResImage removeFromSuperview];
highResImage = nil;
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)viewDidAppear:(BOOL)animated {
@ -246,6 +260,19 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
[AppDelegate theDelegate].masterTabBarController.visibleRoomId = nil;
}
- (void)onAppDidEnterBackground {
// close any opened CustomImageView
if (highResImage) {
[highResImage removeFromSuperview];
highResImage = nil;
}
if (self.imageValidationView) {
[self.imageValidationView removeFromSuperview];
self.imageValidationView = nil;
}
}
#pragma mark - room ID
- (void)setRoomId:(NSString *)roomId {
@ -810,8 +837,6 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
NSString *url = content[@"url"];
if (url.length) {
highResImage = [[CustomImageView alloc] initWithFrame:self.membersView.frame];
highResImage.contentMode = UIViewContentModeScaleAspectFit;
highResImage.backgroundColor = [UIColor blackColor];
highResImage.imageURL = url;
[self.view addSubview:highResImage];
@ -821,7 +846,6 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
[tap setNumberOfTapsRequired:1];
[highResImage addGestureRecognizer:tap];
highResImage.userInteractionEnabled = YES;
highResImage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
}
} else if (msgtype == RoomMessageTypeVideo) {
NSString *url =content[@"url"];
@ -1834,14 +1858,37 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
[weakSelf.actionMenu addActionWithTitle:@"Media" style:CustomAlertActionStyleDefault handler:^(CustomAlert *alert) {
if (weakSelf) {
weakSelf.actionMenu = nil;
// Open media gallery
UIImagePickerController *mediaPicker = [[UIImagePickerController alloc] init];
mediaPicker.delegate = weakSelf;
mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
mediaPicker.allowsEditing = NO;
mediaPicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie, nil];
[[AppDelegate theDelegate].masterTabBarController presentMediaPicker:mediaPicker];
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.actionMenu = [[CustomAlert alloc] initWithTitle:@"Media:" message:nil style:CustomAlertStyleActionSheet];
[weakSelf.actionMenu addActionWithTitle:@"Photo Library" style:CustomAlertActionStyleDefault handler:^(CustomAlert *alert) {
// Open media gallery
UIImagePickerController *mediaPicker = [[UIImagePickerController alloc] init];
mediaPicker.delegate = weakSelf;
mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
mediaPicker.allowsEditing = NO;
mediaPicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie, nil];
[[AppDelegate theDelegate].masterTabBarController presentMediaPicker:mediaPicker];
}];
[weakSelf.actionMenu addActionWithTitle:@"Take Photo or Video" style:CustomAlertActionStyleDefault handler:^(CustomAlert *alert) {
// Open media gallery
UIImagePickerController *mediaPicker = [[UIImagePickerController alloc] init];
mediaPicker.delegate = weakSelf;
mediaPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
mediaPicker.allowsEditing = NO;
mediaPicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie, nil];
[[AppDelegate theDelegate].masterTabBarController presentMediaPicker:mediaPicker];
}];
weakSelf.actionMenu.cancelButtonIndex = [weakSelf.actionMenu addActionWithTitle:@"Cancel" style:CustomAlertActionStyleDefault handler:^(CustomAlert *alert) {
weakSelf.actionMenu = nil;
}];
[weakSelf.actionMenu showInViewController:weakSelf];
});
}
}];
weakSelf.actionMenu.cancelButtonIndex = [weakSelf.actionMenu addActionWithTitle:@"Cancel" style:CustomAlertActionStyleDefault handler:^(CustomAlert *alert) {
weakSelf.actionMenu = nil;
@ -2371,6 +2418,58 @@ NSString *const kCmdResetUserPowerLevel = @"/deop";
} failure:^(NSError *error) {
[self handleError:error forLocalEvent:localEvent];
}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
if (selectedImage) {
__weak typeof(self) weakSelf = self;
// media picker does not offer a preview
// so add a preview to let the user validates his selection
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
self.imageValidationView = [[CustomImageView alloc] initWithFrame:self.membersView.frame];
// the user validates the image
[self.imageValidationView setRightButtonTitle:@"OK" handler:^(CustomImageView* imageView, NSString* buttonTitle) {
// dismiss the image view
[weakSelf.imageValidationView dismissSelection];
[weakSelf.imageValidationView removeFromSuperview];
weakSelf.imageValidationView = nil;
[weakSelf sendImage:selectedImage];
}];
// the user wants to use an other image
[self.imageValidationView setLeftButtonTitle:@"Cancel" handler:^(CustomImageView* imageView, NSString* buttonTitle) {
// dismiss the image view
[weakSelf.imageValidationView dismissSelection];
[weakSelf.imageValidationView removeFromSuperview];
weakSelf.imageValidationView = nil;
// Open again media gallery
UIImagePickerController *mediaPicker = [[UIImagePickerController alloc] init];
mediaPicker.delegate = weakSelf;
mediaPicker.sourceType = picker.sourceType;
mediaPicker.allowsEditing = NO;
mediaPicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie, nil];
[[AppDelegate theDelegate].masterTabBarController presentMediaPicker:mediaPicker];
}];
self.imageValidationView.image = selectedImage;
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:self.imageValidationView];
});
} else {
[weakSelf sendImage:selectedImage];
}
}
} else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
NSURL* selectedVideo = [info objectForKey:UIImagePickerControllerMediaURL];