2014-11-24 09:38:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "CustomImageView.h"
|
|
|
|
#import "MediaManager.h"
|
2014-12-22 10:52:29 +00:00
|
|
|
#import "AppDelegate.h"
|
2014-11-24 09:38:23 +00:00
|
|
|
|
|
|
|
@interface CustomImageView () {
|
|
|
|
id imageLoader;
|
|
|
|
UIActivityIndicatorView *loadingWheel;
|
2014-12-22 10:52:29 +00:00
|
|
|
|
|
|
|
// validation buttons
|
|
|
|
UIButton* leftButton;
|
|
|
|
UIButton* rightButton;
|
|
|
|
|
|
|
|
NSString* leftButtonTitle;
|
|
|
|
NSString* rightButtonTitle;
|
|
|
|
|
|
|
|
blockCustomImageView_onClick leftHandler;
|
|
|
|
blockCustomImageView_onClick rightHandler;
|
|
|
|
|
|
|
|
UIView* bottomBarView;
|
2015-01-06 08:41:44 +00:00
|
|
|
|
|
|
|
// sub items
|
|
|
|
UIScrollView* scrollView;
|
|
|
|
UIImageView* imageView;
|
|
|
|
|
|
|
|
//
|
|
|
|
NSString* loadedImageURL;
|
|
|
|
UIImage* _image;
|
2014-11-24 09:38:23 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CustomImageView
|
2015-01-06 08:41:44 +00:00
|
|
|
@synthesize canBeZoomed;
|
2014-11-24 09:38:23 +00:00
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
if (imageLoader) {
|
|
|
|
[MediaManager cancel:imageLoader];
|
|
|
|
imageLoader = nil;
|
|
|
|
}
|
|
|
|
if (loadingWheel) {
|
|
|
|
[loadingWheel removeFromSuperview];
|
|
|
|
loadingWheel = nil;
|
|
|
|
}
|
2014-12-22 10:52:29 +00:00
|
|
|
if (bottomBarView) {
|
|
|
|
[bottomBarView removeFromSuperview];
|
|
|
|
bottomBarView = nil;
|
|
|
|
}
|
2014-11-28 16:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startActivityIndicator {
|
|
|
|
// Add activity indicator if none
|
|
|
|
if (loadingWheel == nil) {
|
2015-01-06 10:08:29 +00:00
|
|
|
loadingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
|
|
|
|
|
|
|
|
CGRect frame = loadingWheel.frame;
|
|
|
|
frame.size.width += 30;
|
|
|
|
frame.size.height += 30;
|
|
|
|
loadingWheel.bounds = frame;
|
|
|
|
[loadingWheel.layer setCornerRadius:5];
|
2014-11-28 16:02:46 +00:00
|
|
|
[self addSubview:loadingWheel];
|
|
|
|
}
|
2015-01-06 10:08:29 +00:00
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
// Adjust color
|
|
|
|
if ([self.backgroundColor isEqual:[UIColor blackColor]]) {
|
|
|
|
loadingWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
|
2015-01-06 10:08:29 +00:00
|
|
|
// a preview image could be displayed
|
|
|
|
// ensure that the white spinner is visible
|
|
|
|
// it could be drawn on a white area
|
|
|
|
loadingWheel.backgroundColor = [UIColor darkGrayColor];
|
2014-11-28 16:02:46 +00:00
|
|
|
} else {
|
|
|
|
loadingWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
|
|
|
|
}
|
2015-01-06 10:08:29 +00:00
|
|
|
|
|
|
|
// ensure that the spinner is drawn at the top
|
|
|
|
[loadingWheel.superview bringSubviewToFront:loadingWheel];
|
|
|
|
|
|
|
|
// Adjust position
|
|
|
|
CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
|
|
|
|
loadingWheel.center = center;
|
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
// Start
|
|
|
|
[loadingWheel startAnimating];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopActivityIndicator {
|
|
|
|
if (loadingWheel) {
|
|
|
|
[loadingWheel stopAnimating];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2015-01-06 08:41:44 +00:00
|
|
|
// image setter/getter
|
|
|
|
- (void)setImage:(UIImage *)anImage {
|
|
|
|
_image = anImage;
|
|
|
|
|
|
|
|
imageView.image = anImage;
|
|
|
|
[self initScrollZoomFactors];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIImage*)image {
|
|
|
|
return _image;
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
- (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];
|
|
|
|
|
2015-01-05 16:07:41 +00:00
|
|
|
// use the same text color as the tabbar
|
|
|
|
[button setTitleColor:[AppDelegate theDelegate].masterTabBarController.tabBar.tintColor forState:UIControlStateNormal];
|
|
|
|
[button setTitleColor:[AppDelegate theDelegate].masterTabBarController.tabBar.tintColor forState:UIControlStateHighlighted];
|
2014-12-22 10:52:29 +00:00
|
|
|
|
|
|
|
// keep the bottomView background color
|
|
|
|
button.backgroundColor = [UIColor clearColor];
|
|
|
|
|
|
|
|
[button addTarget:self action:@selector(onButtonToggle:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[bottomBarView addSubview:button];
|
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2015-01-06 08:41:44 +00:00
|
|
|
- (void)initScrollZoomFactors {
|
|
|
|
// check if the image can be zoomed
|
|
|
|
if (self.image && self.canBeZoomed && imageView.frame.size.width && imageView.frame.size.height) {
|
|
|
|
// ensure that the content size is properly initialized
|
|
|
|
scrollView.contentSize = scrollView.frame.size;
|
|
|
|
|
|
|
|
// compute the appliable zoom factor
|
|
|
|
// assume that the user does not expect to zoom more than 100%
|
|
|
|
CGSize imageSize = self.image.size;
|
|
|
|
|
|
|
|
CGFloat scaleX = imageSize.width / imageView.frame.size.width;
|
|
|
|
CGFloat scaleY = imageSize.height / imageView.frame.size.height;
|
|
|
|
|
|
|
|
if (scaleX < scaleY)
|
|
|
|
{
|
|
|
|
scaleX = scaleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scaleX < 1.0)
|
|
|
|
{
|
|
|
|
scaleX = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollView.zoomScale = 1.0;
|
|
|
|
scrollView.minimumZoomScale = 1.0;
|
|
|
|
scrollView.maximumZoomScale = scaleX;
|
|
|
|
|
|
|
|
// update the image frame to ensure that it fits to the scrollview frame
|
|
|
|
imageView.frame = scrollView.bounds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
- (void)layoutSubviews {
|
|
|
|
|
|
|
|
// call upper layer
|
|
|
|
[super layoutSubviews];
|
|
|
|
|
2015-01-06 08:41:44 +00:00
|
|
|
// create the subviews if they don't exist
|
|
|
|
if (!scrollView) {
|
|
|
|
scrollView = [[UIScrollView alloc] init];
|
|
|
|
scrollView.delegate = self;
|
|
|
|
scrollView.backgroundColor = [UIColor clearColor];
|
|
|
|
[self addSubview:scrollView];
|
|
|
|
|
|
|
|
imageView = [[UIImageView alloc] init];
|
|
|
|
imageView.backgroundColor = [UIColor clearColor];
|
|
|
|
imageView.contentMode = UIViewContentModeScaleAspectFit;
|
|
|
|
[scrollView addSubview:imageView];
|
|
|
|
}
|
|
|
|
|
|
|
|
// the image has been updated
|
|
|
|
if (imageView.image != self.image) {
|
|
|
|
imageView.image = self.image;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the scrollview frame
|
|
|
|
CGRect oneSelfFrame = CGRectIntegral(self.frame);
|
|
|
|
CGRect scrollViewFrame = CGRectIntegral(scrollView.frame);
|
|
|
|
|
|
|
|
oneSelfFrame.origin = scrollViewFrame.origin = CGPointZero;
|
|
|
|
|
|
|
|
// use integral rect to avoid rounded value issue (float precision)
|
|
|
|
if (!CGRectEqualToRect(CGRectIntegral(self.frame), CGRectIntegral(scrollView.frame))) {
|
|
|
|
scrollView.frame = oneSelfFrame;
|
|
|
|
imageView.frame = oneSelfFrame;
|
|
|
|
|
|
|
|
[self initScrollZoomFactors];
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 10:08:29 +00:00
|
|
|
|
|
|
|
if (loadingWheel.isAnimating) {
|
|
|
|
// ensure that the spinner is drawn at the top
|
|
|
|
[loadingWheel.superview bringSubviewToFront:loadingWheel];
|
|
|
|
|
|
|
|
// Adjust position
|
|
|
|
CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
|
|
|
|
loadingWheel.center = center;
|
|
|
|
}
|
2014-12-22 10:52:29 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 16:02:46 +00:00
|
|
|
- (void)setHideActivityIndicator:(BOOL)hideActivityIndicator {
|
|
|
|
_hideActivityIndicator = hideActivityIndicator;
|
|
|
|
if (hideActivityIndicator) {
|
|
|
|
[self stopActivityIndicator];
|
|
|
|
} else if (imageLoader) {
|
|
|
|
// Loading is in progress, start activity indicator
|
|
|
|
[self startActivityIndicator];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 10:08:29 +00:00
|
|
|
- (void)setImageURL:(NSString *)anImageURL withPreviewImage:(UIImage*)previewImage {
|
2015-01-06 08:41:44 +00:00
|
|
|
// the displayed image is already the expected one ?
|
2015-01-06 10:08:29 +00:00
|
|
|
if ([anImageURL isEqualToString:loadedImageURL]) {
|
2015-01-06 08:41:44 +00:00
|
|
|
|
|
|
|
// check if the image content has not been released
|
|
|
|
if (self.image.size.width && self.image.size.height) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedImageURL = nil;
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:38:23 +00:00
|
|
|
// Cancel media loader in progress (if any)
|
|
|
|
if (imageLoader) {
|
|
|
|
[MediaManager cancel:imageLoader];
|
|
|
|
imageLoader = nil;
|
|
|
|
}
|
|
|
|
|
2015-01-06 10:08:29 +00:00
|
|
|
// preview image until the image is loaded
|
|
|
|
self.image = previewImage;
|
2015-01-06 08:41:44 +00:00
|
|
|
|
2014-11-24 09:38:23 +00:00
|
|
|
// Consider provided url to update image view
|
2015-01-06 10:08:29 +00:00
|
|
|
if (anImageURL) {
|
2014-11-24 09:38:23 +00:00
|
|
|
// Load picture
|
2014-11-28 16:02:46 +00:00
|
|
|
if (!_hideActivityIndicator) {
|
|
|
|
[self startActivityIndicator];
|
|
|
|
}
|
2015-01-06 10:08:29 +00:00
|
|
|
|
|
|
|
imageLoader = [MediaManager loadPicture:anImageURL
|
2015-01-06 08:41:44 +00:00
|
|
|
success:^(UIImage *anImage) {
|
2014-11-28 16:02:46 +00:00
|
|
|
[self stopActivityIndicator];
|
2015-01-06 08:41:44 +00:00
|
|
|
self.image = anImage;
|
2015-01-06 10:08:29 +00:00
|
|
|
loadedImageURL = anImageURL;
|
2014-11-24 09:38:23 +00:00
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
2014-11-28 16:02:46 +00:00
|
|
|
[self stopActivityIndicator];
|
2015-01-06 10:08:29 +00:00
|
|
|
NSLog(@"Failed to download image (%@): %@", anImageURL, error);
|
2014-11-24 09:38:23 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 10:52:29 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 08:41:44 +00:00
|
|
|
#pragma mark - UIScrollViewDelegate
|
|
|
|
// require to be able to zoom an image
|
|
|
|
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
|
|
|
|
{
|
|
|
|
return self.canBeZoomed ? imageView : nil;
|
|
|
|
}
|
|
|
|
|
2014-11-24 09:38:23 +00:00
|
|
|
@end
|