Last active
August 29, 2015 14:06
-
-
Save HeidiHansen/ddc16066e81c4615fb0f to your computer and use it in GitHub Desktop.
04. THEditImageViewController.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "THEditImageViewController.h" | |
#import "THCameraViewController.h" | |
#import "THEditImageViewController+Autolayout.h" | |
typedef void(^ButtonReplacementBlock)(void); | |
@interface THEditImageViewController () <UINavigationControllerDelegate, THCameraDelegateProtocol, UIScrollViewDelegate> | |
@property (nonatomic, assign) CGFloat zoomScale; | |
@property (nonatomic, assign) CGFloat maximumZoomScale; | |
@property (nonatomic, assign) CGFloat minimumZoomScale; | |
@property (nonatomic, assign) CGPoint contentOffset; | |
@property (nonatomic, assign) UIEdgeInsets contentInset; | |
@end | |
@implementation THEditImageViewController | |
#pragma mark - Getters | |
-(NSArray *)baseToolbarItems | |
{ | |
UIImage *switchIcon; | |
SEL splitSelector; | |
if (self.horizontalSplit){ | |
switchIcon = [UIImage imageNamed:@"1074-grid-2B rotated"]; | |
splitSelector = @selector(switchImagesAcrossHorizontalSplit); | |
} | |
else { | |
switchIcon = [UIImage imageNamed:@"1074-grid-2"]; | |
splitSelector = @selector(switchImagesAcrossVerticalSplit); | |
} | |
UIBarButtonItem *switchSubviewsButton = [[UIBarButtonItem alloc] initWithImage:switchIcon | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:splitSelector]; | |
UIBarButtonItem *textOverlay = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"1174-choose-font-toolbar"] | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(textOverlayTapped)]; | |
UIBarButtonItem *polaroidFrameButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"polaroidIcon.png"] | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(didTapPolaroidIcon:)]; | |
UIBarButtonItem *stickerButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"1169-star-toolbar"] | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(stickerTapped)]; | |
UIBarButtonItem *switchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"759-refresh-2"] | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(changeSplit)]; | |
return _baseToolbarItems = @[switchSubviewsButton, self.spacerBBI, | |
switchButton, self.spacerBBI, | |
textOverlay, self.spacerBBI, | |
polaroidFrameButton, self.spacerBBI, | |
stickerButton]; | |
} | |
-(void)changeSplit | |
{ | |
self.horizontalSplit = !self.horizontalSplit; | |
if (self.horizontalSplit){ | |
[self animateLayoutHorizontalSplitOfContainerViewsWithCompletion:nil]; | |
} | |
else { | |
[self animateLayoutVerticalSplitOfContainerViewsWithCompletion:nil]; | |
} | |
[self replaceToolbarWithButtons:self.baseToolbarItems]; | |
} | |
-(NSArray *)stickerToolbarItems | |
{ | |
if (!_stickerToolbarItems){ | |
NSMutableArray *toolbarArray = [[NSMutableArray alloc] init]; | |
[toolbarArray addObject:self.returnButton]; | |
_stickerToolbarItems = toolbarArray; | |
} | |
return _stickerToolbarItems; | |
} | |
-(NSArray *)textToolbarItems | |
{ | |
if (!_textToolbarItems){ | |
NSMutableArray *toolbarArray = [[NSMutableArray alloc] init]; | |
[toolbarArray addObject:self.returnButton]; | |
UIBarButtonItem *deleteBBI = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"711-trash"] | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(hideText)]; | |
UIBarButtonItem *typefaceBBI = [[UIBarButtonItem alloc] initWithTitle:@"Typeface" | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(textOverlayTapped)]; | |
UIBarButtonItem *textColorBBI = [[UIBarButtonItem alloc] initWithTitle:@"TextColor" | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(textColorButtonTapped)]; | |
[toolbarArray addObjectsFromArray:@[self.spacerBBI, deleteBBI, | |
self.spacerBBI, typefaceBBI, | |
self.spacerBBI, textColorBBI, | |
self.spacerBBI]]; | |
_textToolbarItems = toolbarArray; | |
} | |
return _textToolbarItems; | |
} | |
- (void)createSecondaryToolbarWithColors | |
{ | |
[self layoutSecondaryToolbarWithButtonsArray:self.fontColorButtonArray]; | |
[self.view layoutIfNeeded]; | |
} | |
- (void)hideText | |
{ | |
self.thenLabel.hidden = YES; | |
self.nowLabel.hidden = YES; | |
} | |
- (UIBarButtonItem *)returnButton | |
{ | |
if (!_returnButton) { | |
_returnButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"433-x"] | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(returnTapped)]; | |
} | |
return _returnButton; | |
} | |
-(UIBarButtonItem *)spacerBBI | |
{ | |
if (!_spacerBBI) { | |
_spacerBBI = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace | |
target:nil | |
action:nil]; | |
} | |
return _spacerBBI; | |
} | |
-(NSDictionary *)metrics | |
{ | |
NSNumber *thenImageHeight = [NSNumber numberWithFloat:self.thenImage.size.height]; | |
NSNumber *thenImageWidth = [NSNumber numberWithFloat:self.thenImage.size.width]; | |
NSNumber *nowImageHeight = [NSNumber numberWithFloat:self.nowImage.size.height]; | |
NSNumber *nowImageWidth = [NSNumber numberWithFloat:self.nowImage.size.width]; | |
NSNumber *typefaceToolbarWidth = [self determineWidthOfSecondaryToolbarWithButtons:self.typefaceButtonArray | |
usingIndividualButtonWidthBlock:^NSInteger(UIButton *button){ | |
__block CGSize buttonSize; | |
[button.titleLabel.attributedText enumerateAttributesInRange:NSMakeRange(0,[button.titleLabel.text length]) | |
options:NSAttributedStringEnumerationReverse | |
usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) { | |
buttonSize = [button.titleLabel.text sizeWithAttributes:attributes]; | |
}]; | |
NSString *buttonWidth = [NSString stringWithFormat:@"%d",(int)roundf(ceilf(buttonSize.width))]; | |
return [buttonWidth intValue]; | |
}]; | |
NSInteger phoneWidth = [[UIScreen mainScreen] bounds].size.width; | |
NSInteger phoneHeight = [[UIScreen mainScreen] bounds].size.height; | |
NSNumber *fontColorToolbarWidth = @1500; | |
NSString *kNavAndStatusBarHeight = @"navAndStatusBarHeight"; | |
NSString *kZeroHeight = @"zeroHeight"; | |
NSString *kCameraHeight = @"cameraHeight"; | |
NSString *kToolbarHeight = @"toolbarHeight"; | |
NSString *kContainerWidth = @"containerWidth"; | |
NSString *kContainerHeight = @"containerHeight"; | |
NSString *kTypefaceToolbarWidth = @"typefaceToolbarWidth"; | |
NSString *kFontColorToolbarWidth = @"fontColorToolbarWidth"; | |
NSString *kContainerWidthReduced = @"containerWidthReduced"; | |
NSString *kContainerHeightReduced = @"containerHeightReduced"; | |
NSString *kTopOrBottomInset = @"topOrBottomInset"; | |
NSString *kLeftOrRightExtraInset = @"leftOrRightExtraInset"; | |
NSString *kTopOrBottomExtraInset = @"topOrBottomExtraInset"; | |
NSString *kTopOrBottomExtraInsetDelta = @"topOrBottomExtraInsetDelta"; | |
NSString *kLeftOrRightExtraInsetDelta = @"leftOrRightExtraInsetDelta"; | |
NSNumber *zeroHeight = @0; | |
NSNumber *cameraHeight = @(phoneWidth *5/4); | |
NSNumber *containerWidth; | |
NSNumber *containerHeight; | |
if (self.horizontalSplit){ | |
containerWidth = @(phoneWidth); | |
containerHeight = @(phoneWidth/2); | |
} | |
else{ | |
containerWidth = @(phoneWidth/2); | |
containerHeight = @(phoneWidth); | |
} | |
NSNumber *topOrBottomInset = @((phoneHeight - phoneWidth)/2); | |
NSNumber *topOrBottomExtraInsetDelta = @((15.0/568.0) *phoneHeight); | |
NSNumber *leftOrRightExtraInsetDelta = @((15.0/320.0) *phoneWidth); | |
NSNumber *leftOrRightExtraInset = leftOrRightExtraInsetDelta; | |
NSNumber *topOrBottomExtraInset = @([topOrBottomInset integerValue] + [topOrBottomExtraInsetDelta integerValue]); | |
NSNumber *containerWidthReduced = @([containerWidth integerValue] - 2*[leftOrRightExtraInsetDelta integerValue]); | |
NSNumber *containerHeightReduced = @([containerHeight integerValue] - 2*[topOrBottomExtraInsetDelta integerValue]); | |
NSNumber *navAndStatusBarHeight; | |
NSNumber *toolbarHeight; | |
if (phoneHeight == 568){ | |
navAndStatusBarHeight = @64; | |
toolbarHeight = @44; | |
} | |
else if (phoneHeight == 480){ | |
navAndStatusBarHeight = @64; | |
toolbarHeight = @44; | |
} | |
_metrics = @{kNavAndStatusBarHeight:navAndStatusBarHeight, | |
kZeroHeight:zeroHeight, | |
kToolbarHeight:toolbarHeight, | |
kCameraHeight:cameraHeight, | |
@"thenImageHeight":thenImageHeight, | |
@"thenImageWidth":thenImageWidth, | |
@"nowImageHeight":nowImageHeight, | |
@"nowImageWidth":nowImageWidth, | |
kTypefaceToolbarWidth:typefaceToolbarWidth, | |
kFontColorToolbarWidth:fontColorToolbarWidth, | |
kContainerHeight:containerHeight, | |
kContainerWidth:containerWidth, | |
kTopOrBottomInset:topOrBottomInset, | |
kContainerWidthReduced:containerWidthReduced, | |
kContainerHeightReduced:containerHeightReduced, | |
kLeftOrRightExtraInset:leftOrRightExtraInset, | |
kTopOrBottomExtraInset:topOrBottomExtraInset, | |
kTopOrBottomExtraInsetDelta:topOrBottomExtraInsetDelta}; | |
return _metrics; | |
} | |
- (UIImageView *)nowImageView | |
{ | |
if (self.nowImage){ | |
[_nowImageView setContentMode:UIViewContentModeScaleAspectFit]; | |
[_nowImageView setImage:self.nowImage]; | |
} | |
return _nowImageView; | |
} | |
-(NSDictionary *)topBottomViewsDictionary | |
{ | |
id _cameraView = self.cameraContainerView; | |
id _topLayoutGuide = self.topLayoutGuide; | |
id _topContainerView; | |
id _bottomContainerView; | |
if (self.thenOnLeftOrTop){ | |
_topContainerView = self.thenContainerView; | |
_bottomContainerView = self.nowContainerView; | |
} | |
else{ | |
_bottomContainerView = self.thenContainerView; | |
_topContainerView = self.nowContainerView; | |
} | |
_topBottomViewsDictionary = NSDictionaryOfVariableBindings(_topContainerView, _bottomContainerView, _toolbar, _topLayoutGuide, _cameraView); | |
return _topBottomViewsDictionary; | |
} | |
-(NSDictionary *)leftRightViewsDictionary | |
{ | |
id _leftContainerView; | |
id _rightContainerView; | |
id _cameraView = self.cameraContainerView; | |
if (self.thenOnLeftOrTop){ | |
_leftContainerView = self.thenContainerView; | |
_rightContainerView = self.nowContainerView; | |
} | |
else{ | |
_rightContainerView = self.thenContainerView; | |
_leftContainerView = self.nowContainerView; | |
} | |
_leftRightViewsDictionary = NSDictionaryOfVariableBindings(_leftContainerView, _rightContainerView,_cameraView,_toolbar); | |
return _leftRightViewsDictionary; | |
} | |
-(NSDictionary *)subviewsDictionary | |
{ | |
if (!_subviewsDictionary) { | |
_subviewsDictionary = NSDictionaryOfVariableBindings(_thenImageView, _nowImageView, _thenScrollView, _nowScrollView); | |
} | |
return _subviewsDictionary; | |
} | |
-(NSDictionary *)labelsDictionary | |
{ | |
if (!_labelsDictionary) { | |
_labelsDictionary = NSDictionaryOfVariableBindings(_thenLabel, _nowLabel); | |
} | |
return _labelsDictionary; | |
} | |
#pragma mark - Lifecycle | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self layoutPreviewPanel]; | |
} | |
- (void)layoutPreviewPanel | |
{ | |
self.navigationController.navigationBar.hidden = NO; | |
self.navigationController.navigationBar.alpha = 1; | |
self.thenOnLeftOrTop = YES; | |
self.thenImage = self.imageToProcess; | |
self.takingPhoto = NO; | |
self.horizontalSplit = NO; | |
[self baseInit]; | |
[self removeAllTopLevelViewConstraints]; | |
self.nowScrollView.layer.backgroundColor = [UIColor blackColor].CGColor; | |
self.thenScrollView.layer.backgroundColor = [UIColor blackColor].CGColor; | |
[self layoutThenAndNowContainerViews]; | |
[self layoutVerticalSplitOfContainerViewsWithFullScreenThenImage]; | |
[self setupPreviewNavbar]; | |
self.cameraContainerView.hidden = YES; | |
[self setupCamera]; | |
[self setupInitialStateOfImageViews]; | |
[self.view layoutIfNeeded]; | |
} | |
- (void)setupPreviewNavbar | |
{ | |
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] | |
initWithTitle:@"Close" | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(closeTapped)]; | |
self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithTitle:@"Edit" | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(editTapped)]]; | |
} | |
#pragma mark - Navbar Setup | |
- (void)setupBaseNavbar | |
{ | |
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" | |
style:UIBarButtonItemStylePlain | |
target:self | |
action:@selector(doneButtonTapped)]; | |
self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera | |
target:self | |
action:@selector(cameraTapped:)]]; | |
} | |
- (void)editTapped | |
{ | |
[self animateLayoutVerticalSplitOfContainerViewsWithCompletion:^{ | |
[self setupBaseNavbar]; | |
}]; | |
} | |
- (void)layoutEditingPanel | |
{ | |
self.navigationController.navigationBar.hidden = NO; | |
self.navigationController.navigationBar.alpha = 1; | |
self.thenOnLeftOrTop = YES; | |
self.thenImage = self.imageToProcess; | |
self.takingPhoto = NO; | |
self.horizontalSplit = NO; | |
[self baseInit]; | |
[self removeAllTopLevelViewConstraints]; | |
self.nowScrollView.layer.backgroundColor = [UIColor blackColor].CGColor; | |
self.thenScrollView.layer.backgroundColor = [UIColor blackColor].CGColor; | |
[self layoutThenAndNowContainerViews]; | |
[self setupBaseNavbar]; | |
self.cameraContainerView.hidden = YES; | |
[self setupCamera]; | |
[self setupInitialStateOfImageViews]; | |
[self.view layoutIfNeeded]; | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
//[self resetZoomScaleAndContentOffset]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
} | |
- (void)baseInit | |
{ | |
self.nowContainerView = [[UIView alloc] init]; | |
self.thenContainerView = [[UIView alloc] init]; | |
self.nowScrollView = [[UIScrollView alloc] init]; | |
self.thenScrollView = [[UIScrollView alloc] init]; | |
self.nowScrollView.delegate = self; | |
self.thenScrollView.delegate = self; | |
CGFloat widthDivisor = 160.0; | |
CGFloat heightDivisor = 320.0; | |
CGFloat thenMinZoomScale = MAX(widthDivisor/self.thenImage.size.width,heightDivisor/self.thenImage.size.height); | |
self.thenScrollView.maximumZoomScale = 4; | |
self.thenScrollView.minimumZoomScale = 0.2; | |
self.nowScrollView.showsHorizontalScrollIndicator = NO; | |
self.nowScrollView.showsVerticalScrollIndicator = NO; | |
self.thenScrollView.showsHorizontalScrollIndicator = NO; | |
self.thenScrollView.showsVerticalScrollIndicator = NO; | |
self.nowContainerView.layer.borderColor = [UIColor whiteColor].CGColor; | |
self.nowContainerView.layer.borderWidth = 3; | |
self.thenContainerView.layer.borderColor = [UIColor whiteColor].CGColor; | |
self.thenContainerView.layer.borderWidth = 3; | |
self.cameraContainerView = [[UIView alloc] init]; | |
self.cameraContainerView.hidden = YES; | |
self.contentViewForThenImage = [[UIView alloc] init]; | |
self.contentViewForNowImage = [[UIView alloc] init]; | |
self.thenImageView = [[UIImageView alloc] init]; | |
self.nowImageView = [[UIImageView alloc] init]; | |
self.thenImageView.contentMode = UIViewContentModeCenter; | |
self.toolbar = [[UIToolbar alloc] init]; | |
// self.pictureAddition = [[THPictureAddition alloc] init]; | |
[self.view addSubview:self.thenContainerView]; | |
[self.view addSubview:self.nowContainerView]; | |
[self.view addSubview:self.toolbar]; | |
[self.view addSubview:self.cameraContainerView]; | |
[self.thenContainerView addSubview:self.thenScrollView]; | |
[self.nowContainerView addSubview:self.nowScrollView]; | |
[self.thenScrollView addSubview:self.thenImageView]; | |
[self.nowScrollView addSubview:self.nowImageView]; | |
[self.thenScrollView setBackgroundColor:[UIColor blackColor]]; | |
[self.nowScrollView setBackgroundColor:[UIColor blackColor]]; | |
[self.toolbar setItems:self.baseToolbarItems]; | |
} | |
-(void)viewDidLayoutSubviews | |
{ | |
} | |
- (void)setupCamera | |
{ | |
if (self.cameraVC){ | |
[self.cameraVC removeFromParentViewController]; | |
} | |
self.cameraVC = [[THCameraViewController alloc] init]; | |
[self addChildViewController:self.cameraVC]; | |
[self.cameraContainerView addSubview:self.cameraVC.view]; | |
self.cameraContainerView.frame = self.view.bounds; | |
self.cameraVC.view.frame = self.cameraContainerView.bounds; | |
self.cameraVC.delegate = self; | |
} | |
- (void)setupInitialStateOfImageViews | |
{ | |
self.thenImage = self.imageToProcess; | |
[self.thenImageView setContentMode:UIViewContentModeScaleAspectFit]; | |
[self.thenImageView setImage:self.thenImage]; | |
} | |
- (void)replaceToolbarWithButtons:(NSArray *)buttons | |
{ | |
[self animateLayoutToolbarOfHeightZeroAtBottomOfScreenWithCompletion:^{ | |
[self.toolbar setItems:buttons animated:NO]; | |
[self animateLayoutToolbarOfStandardHeightWithCompletion:^{ | |
NSLog(@"Completed toolbar button update."); | |
}]; | |
}]; | |
} | |
- (void)resignCamera | |
{ | |
[self.toolbar setItems:self.baseToolbarItems animated:NO]; | |
[self animateCameraResignWithSetupViewsBlock:^{ | |
[self layoutThenAndNowContainerViews]; | |
} AndCompletion:^{ | |
[self setupCamera]; | |
self.cameraContainerView.hidden = YES; | |
[self.view layoutIfNeeded]; | |
}]; | |
} | |
#pragma mark - Segue | |
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if ([segue.identifier isEqualToString:@"cameraSegue"]){ | |
self.cameraVC = segue.destinationViewController; | |
self.cameraVC.delegate = self; | |
} | |
} | |
#pragma mark - CoreGraphics | |
- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage | |
{ | |
UIImage *image = nil; | |
CGSize newImageSize = CGSizeMake(firstImage.size.width + secondImage.size.width, MAX(firstImage.size.height, secondImage.size.height)); | |
if (UIGraphicsBeginImageContextWithOptions != NULL) { | |
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]); | |
} | |
else { | |
UIGraphicsBeginImageContext(newImageSize); | |
} | |
[firstImage drawAtPoint:CGPointMake(0, 0)]; | |
[secondImage drawAtPoint:CGPointMake(firstImage.size.width,0 )]; | |
image = UIGraphicsGetImageFromCurrentImageContext(); | |
NSLog(@"New Image Size : (%f, %f)", image.size.width, image.size.height); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
- (void)setupCameraNavigationBar | |
{ | |
self.navigationItem.rightBarButtonItem = nil; | |
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel | |
target:self | |
action:@selector(resignCamera)]; | |
} | |
#pragma mark - buttonTaps | |
- (void)cameraTapped:(id)sender | |
{ | |
[self layoutCamera]; | |
[self setupCameraNavigationBar]; | |
} | |
- (void)returnTapped | |
{ | |
self.secondaryToolbar.hidden = YES; | |
[self replaceToolbarWithButtons:self.baseToolbarItems]; | |
} | |
- (void)stickerTapped | |
{ | |
[self replaceToolbarWithButtons:self.stickerToolbarItems]; | |
} | |
- (void)textOverlayTapped | |
{ | |
self.secondaryToolbar.hidden = NO; | |
self.thenLabel.hidden = NO; | |
self.nowLabel.hidden = NO; | |
[self replaceToolbarWithButtons:self.textToolbarItems]; | |
if (self.secondaryToolbar){ | |
[self.secondaryToolbar removeFromSuperview]; | |
[self.contentViewForSecondaryToolbar removeFromSuperview]; | |
} | |
self.secondaryToolbar = [[UIScrollView alloc] init]; | |
[self.view addSubview:self.secondaryToolbar]; | |
[self.secondaryToolbar setShowsHorizontalScrollIndicator:NO]; | |
[self.secondaryToolbar setShowsVerticalScrollIndicator:NO]; | |
self.contentViewForSecondaryToolbar = [[UIView alloc] init]; | |
[self.secondaryToolbar addSubview:self.contentViewForSecondaryToolbar]; | |
[self layoutSecondaryToolbarWithButtonsArray:self.typefaceButtonArray]; | |
[self typefaceButtonTapped]; | |
} | |
- (void)textColorButtonTapped | |
{ | |
self.secondaryToolbar.hidden = NO; | |
self.thenLabel.hidden = NO; | |
self.nowLabel.hidden = NO; | |
if (self.secondaryToolbar){ | |
[self.secondaryToolbar removeFromSuperview]; | |
[self.contentViewForSecondaryToolbar removeFromSuperview]; | |
} | |
self.secondaryToolbar = [[UIScrollView alloc] init]; | |
[self.view addSubview:self.secondaryToolbar]; | |
[self.secondaryToolbar setShowsHorizontalScrollIndicator:NO]; | |
[self.secondaryToolbar setShowsVerticalScrollIndicator:NO]; | |
self.contentViewForSecondaryToolbar = [[UIView alloc] init]; | |
[self.secondaryToolbar addSubview:self.contentViewForSecondaryToolbar]; | |
[self layoutSecondaryToolbarWithButtonsArray:self.fontColorButtonArray]; | |
[self colorTANTextWithFontColor:nil]; | |
self.secondaryToolbar.backgroundColor = [UIColor whiteColor]; | |
self.secondaryToolbar.alpha = 0.85; | |
[self.secondaryToolbar layoutIfNeeded]; | |
} | |
- (void)typefaceButtonTapped | |
{ | |
self.secondaryToolbar.hidden = NO; | |
self.thenLabel.hidden = NO; | |
self.nowLabel.hidden = NO; | |
[self showHideTANTextWithFont:nil]; | |
self.secondaryToolbar.backgroundColor = [UIColor whiteColor]; | |
self.secondaryToolbar.alpha = 0.85; | |
[self.secondaryToolbar layoutIfNeeded]; | |
} | |
- (void)fontColorButtonTapped:(id)sender | |
{ | |
self.secondaryToolbar.hidden = NO; | |
self.thenLabel.hidden = NO; | |
self.nowLabel.hidden = NO; | |
[self colorTANTextWithFontColor:sender]; | |
self.secondaryToolbar.backgroundColor = [UIColor whiteColor]; | |
self.secondaryToolbar.alpha = 0.85; | |
[self.view layoutIfNeeded]; | |
} | |
- (void)animateTypefaceChange:(id)sender | |
{ | |
[UIView animateWithDuration:0.2 animations:^{ | |
if (!self.thenLabel.hidden){ | |
self.nowLabel.alpha = 0; | |
self.thenLabel.alpha = 0; | |
} | |
else{ | |
self.nowLabel.hidden = NO; | |
self.thenLabel.hidden = NO; | |
} | |
} completion:^(BOOL finished){ | |
[self showHideTANTextWithFont:sender]; | |
[UIView animateWithDuration:0.2 animations:^{ | |
self.thenLabel.alpha = 1; | |
self.nowLabel.alpha = 1; | |
} | |
completion:^(BOOL finished) { | |
}]; | |
}]; | |
} | |
- (void)colorTANTextWithFontColor:(id)sender | |
{ | |
UIButton *senderButton = (UIButton *)sender; | |
if ([sender isKindOfClass:[UIButton class]]){ | |
[self.thenLabel.attributedText enumerateAttributesInRange:NSMakeRange(0,[self.thenLabel.attributedText length]) | |
options:NSAttributedStringEnumerationReverse | |
usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) { | |
__block UIColor *color; | |
[senderButton.titleLabel.attributedText enumerateAttributesInRange:NSMakeRange(0,1) | |
options:NSAttributedStringEnumerationReverse | |
usingBlock: | |
^(NSDictionary *attributes, NSRange range, BOOL *stop) { | |
color = attributes[@"NSColor"]; | |
}]; | |
NSMutableDictionary *mutableAttributes = [attributes mutableCopy]; | |
mutableAttributes[@"NSColor"] = color; | |
self.chosenColor = color; | |
NSString *textForAttributedThenText = @"Then"; | |
NSString *textForAttributedNowText = @"Now"; | |
NSMutableAttributedString *attributedThenText = | |
[[NSMutableAttributedString alloc] initWithString:textForAttributedThenText | |
attributes:mutableAttributes]; | |
NSMutableAttributedString *attributedNowText = | |
[[NSMutableAttributedString alloc] initWithString:textForAttributedNowText | |
attributes:mutableAttributes]; | |
self.thenLabel.attributedText = attributedThenText; | |
self.nowLabel.attributedText = attributedNowText; | |
}]; | |
} | |
} | |
- (UIColor *)colorFromImage:(UIImage *)image xCoordinate:(int)x yCoordinate:(int)y | |
{ | |
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); | |
const UInt8* data = CFDataGetBytePtr(pixelData); | |
int pixelInfo = ((image.size.width * y) + x ) * 4; | |
UInt8 red = data[pixelInfo]; | |
UInt8 green = data[(pixelInfo + 1)]; | |
UInt8 blue = data[pixelInfo + 2]; | |
CFRelease(pixelData); | |
//UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f]; // The pixel color info | |
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1]; | |
return color; | |
} | |
- (void)showHideTANTextWithFont:(id)sender | |
{ | |
UIColor *textColor; | |
if (!self.nowLabel){ | |
self.nowLabel = [[UILabel alloc] init]; | |
self.thenLabel = [[UILabel alloc] init]; | |
[self.nowContainerView addSubview:self.nowLabel]; | |
[self.thenContainerView addSubview:self.thenLabel]; | |
[self.nowContainerView bringSubviewToFront:self.nowLabel]; | |
[self.thenContainerView bringSubviewToFront:self.thenLabel]; | |
[self layoutTextLabels]; | |
} | |
if (self.chosenColor){ | |
textColor = self.chosenColor; | |
} | |
else{ | |
textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]; | |
} | |
UIButton *senderButton = (UIButton *)sender; | |
if (!self.labelsFont || senderButton){ | |
self.labelsFont = [UIFont fontWithName:senderButton.titleLabel.text size:30.0]; | |
} | |
else if (!senderButton && !self.labelsFont){ | |
self.labelsFont = [UIFont fontWithName:@"Georgia-Bold" size:30.0]; | |
} | |
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; | |
paragraphStyle.alignment = NSTextAlignmentLeft; | |
NSDictionary *attr = @{NSForegroundColorAttributeName:textColor, NSFontAttributeName:self.labelsFont, NSParagraphStyleAttributeName:paragraphStyle}; | |
NSString *textForAttributedThenText = @"Then"; | |
NSString *textForAttributedNowText = @"Now"; | |
NSMutableAttributedString *attributedThenText = | |
[[NSMutableAttributedString alloc] initWithString:textForAttributedThenText | |
attributes:attr]; | |
NSMutableAttributedString *attributedNowText = | |
[[NSMutableAttributedString alloc] initWithString:textForAttributedNowText | |
attributes:attr]; | |
self.thenLabel.attributedText = attributedThenText; | |
self.nowLabel.attributedText = attributedNowText; | |
} | |
#pragma mark - FrameDelegate | |
- (void)didTapPolaroidIcon:(id)sender | |
{ | |
CGRect rect = CGRectMake(0, 0, 320, 320); | |
UIImageView *combinedImageView = [[UIImageView alloc] initWithFrame:rect]; | |
// UIImage *resizedimage = [self.pictureAddition resizeImage:self.nowImage ForPolaroidFrame:rect]; | |
// UIImage *combinedImage = [self.pictureAddition imageByCombiningImage:[UIImage imageNamed:@"polaroidFrame.png"] withImage:resizedimage secondImagePlacement:CGPointMake(20.0,16.0)]; | |
// combinedImageView.image = combinedImage; | |
[self.view addSubview:combinedImageView]; | |
} | |
#pragma mark - CameraDelegate | |
-(void)takePhotoTapped:(UIImage *)image | |
{ | |
if (image){ | |
self.nowImage = image; | |
self.nowImageView.image = self.nowImage; | |
CGFloat widthDivisor = 160.0; | |
CGFloat heightDivisor = 320.0; | |
CGFloat nowMinZoomScale = MAX(widthDivisor/self.nowImage.size.width,heightDivisor/self.nowImage.size.height); | |
self.nowScrollView.maximumZoomScale = 4; | |
self.nowScrollView.minimumZoomScale = 0.2; | |
} | |
[self resignCamera]; | |
} | |
#pragma mark - TextOverlayDelegate | |
- (void)setTextOverlayToImages | |
{ | |
// [self.thenImageView setImage:[self.pictureAddition applyTextOverlayToImage:self.thenImage Position:CGPointMake(0,0) TextSize:200.0 Text:@"Then"] forState:UIControlStateNormal]; | |
// [self.nowImageView setImage:[self.pictureAddition applyTextOverlayToImage:self.nowImage Position:CGPointMake(0,0) TextSize:200.0 Text:@"Now"] forState:UIControlStateNormal]; | |
} | |
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView | |
{ | |
if (scrollView == self.thenScrollView){ | |
return self.thenImageView; | |
}; | |
return self.nowImageView; | |
} | |
- (NSArray *)typefaceButtonArray | |
{ | |
if (!_typefaceButtonArray){ | |
NSArray *typefaceArray = @[@"Georgia-Bold",@"Verdana",@"Papyrus", @"SnellRoundhand-Black", @"Zapfino"]; | |
NSMutableArray *buttonArray = [[NSMutableArray alloc] init]; | |
for (NSInteger i = 0; i < [typefaceArray count]; i++){ | |
UIButton *typeFaceButton = [UIButton buttonWithType:UIButtonTypeCustom]; | |
UIColor *textColor = [UIColor colorWithRed:0 green:0 blue:1.0 alpha:1.0]; | |
NSString *fontName = (NSString *)typefaceArray[i]; | |
UIFont *font = [UIFont fontWithName:fontName size:20.0]; | |
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; | |
paragraphStyle.alignment = NSTextAlignmentLeft; | |
NSDictionary *attr = @{NSForegroundColorAttributeName:textColor, NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle}; | |
NSString *textForAttributedText = (NSString *)typefaceArray[i]; | |
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:textForAttributedText attributes:attr]; | |
[typeFaceButton setAttributedTitle:attributedText forState:UIControlStateNormal]; | |
[typeFaceButton addTarget:self action:@selector(animateTypefaceChange:) forControlEvents:UIControlEventTouchUpInside]; | |
[buttonArray addObject:typeFaceButton]; | |
} | |
_typefaceButtonArray = [[NSArray alloc] initWithArray:buttonArray]; | |
} | |
return _typefaceButtonArray; | |
} | |
- (NSArray *)fontColorButtonArray | |
{ | |
if (!_fontColorButtonArray){ | |
NSMutableArray *buttonArray = [[NSMutableArray alloc] init]; | |
for (NSInteger i = 0; i < 25; i++){ | |
UIButton *fontColorButton = [UIButton buttonWithType:UIButtonTypeCustom]; | |
UIColor *color = [UIColor colorWithRed:arc4random_uniform(255.0)/255.0 | |
green:arc4random_uniform(255.0)/255.0 | |
blue:arc4random_uniform(255.0)/255.0 | |
alpha:1.0]; | |
UIColor *textColor = color; | |
UIFont *font = [UIFont systemFontOfSize:20.0]; | |
NSDictionary *attr = @{NSForegroundColorAttributeName:textColor, NSFontAttributeName:font}; | |
NSString *textForAttributedText = @"AAA"; | |
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:textForAttributedText attributes:attr]; | |
[fontColorButton setAttributedTitle:attributedText forState:UIControlStateNormal]; | |
fontColorButton.backgroundColor = color; | |
[fontColorButton addTarget:self action:@selector(colorTANTextWithFontColor:) forControlEvents:UIControlEventTouchUpInside]; | |
[buttonArray addObject:fontColorButton]; | |
[self.buttonColorArray addObject:color]; | |
} | |
_fontColorButtonArray = [[NSArray alloc] initWithArray:buttonArray]; | |
} | |
return _fontColorButtonArray; | |
} | |
-(NSMutableArray *)buttonColorArray | |
{ | |
if(!_buttonColorArray){ | |
_buttonColorArray = [[NSMutableArray alloc] init]; | |
} | |
return _buttonColorArray; | |
} | |
- (NSNumber *)determineWidthOfSecondaryToolbarWithButtons:(NSArray *)buttonsArray | |
usingIndividualButtonWidthBlock:( NSInteger (^)(UIButton *))buttonWidthBlock; | |
{ | |
NSInteger widthOfSecondaryToolbar = 20; | |
for (UIButton *button in buttonsArray){ | |
NSInteger individualButton = buttonWidthBlock(button); | |
widthOfSecondaryToolbar = individualButton + widthOfSecondaryToolbar + 20; | |
} | |
return @(widthOfSecondaryToolbar); | |
} | |
- (void)closeTapped | |
{ | |
[self dismissViewControllerAnimated:YES completion:nil]; | |
} | |
- (void)doneButtonTapped | |
{ | |
self.savedImage = [self generateFinalImage]; | |
NSLog(@"Saved Image %@", self.savedImage); | |
[self.delegate setFilteredImage:self.savedImage]; | |
[self dismissViewControllerAnimated:YES completion:nil]; | |
} | |
- (UIImage *)generateFinalImage | |
{ | |
CGRect grabRect = CGRectMake(0,124,320,320); | |
//for retina displays | |
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { | |
UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale); | |
} | |
else { | |
UIGraphicsBeginImageContext(grabRect.size); | |
} | |
CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y); | |
[self.view.layer renderInContext:ctx]; | |
return UIGraphicsGetImageFromCurrentImageContext(); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola