Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a7ff434
Makes the toolbar height a constant, placing it in GKImageCropView fo…
Nov 10, 2013
8650012
Makes style of image cropper more iOS7-ish
Nov 10, 2013
2aa7d24
Stops taking toolbar height into consideration with cropper, as it is…
Nov 10, 2013
63bcb2e
Makes the border of the cropper a bit simpler and more explicit
Nov 10, 2013
a1364fc
Modifies placement of imageView on scrollView so that there never are…
Nov 11, 2013
b59f47d
Includes an action sheet for choosing between the camera or photo lib…
Nov 11, 2013
7d6ac41
Replaces deprecated call to presentModalViewController
Nov 11, 2013
01b766a
Show and Hide status bar when displaying the cropping view controller
Nov 12, 2013
9380327
Normalizes crop sizes that may bleed out of the edges of the screen o…
Nov 13, 2013
0b968b8
Brings back the transparent gray fill outside of the crop rect
Nov 13, 2013
1bf5ee7
Keys of localized strings are now english fallbacks (in case a string…
Nov 14, 2013
df58519
Adds podspec and modifies README
Nov 14, 2013
5d96856
Fixes README
Nov 14, 2013
6d40b91
Renames podspec and updates Readme
Nov 14, 2013
3a65c06
Hopefully validates podspec
Nov 14, 2013
50ce1c5
Fixes warnings
Nov 14, 2013
1791cb3
Fixes deployment target
Nov 14, 2013
74e8d19
Fixes deprecated method
Nov 14, 2013
ae02563
Fixes for cocoapods
Nov 14, 2013
4240ba5
Fixes another backwards compatibility issue
Nov 14, 2013
842f6bc
Clips image so it doesn’t show out of bounds while view controller is…
Nov 16, 2013
1d99309
Updates Podspec
Nov 16, 2013
71ee620
Shows action sheet from toolbar, if present (otherwise the cancel but…
Nov 17, 2013
f9366bf
updates podspec
Nov 17, 2013
1737b1c
removed hardcoded width, set it to screen width
timmolter Dec 4, 2014
90b2920
Merge pull request #2 from Tramino/master
pxlshpr Feb 21, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions GKClasses/GKImageCropOverlayView.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "GKImageCropOverlayView.h"
#import "GKImageCropView.h"

@interface GKImageCropOverlayView ()
@property (nonatomic, strong) UIToolbar *toolbar;
Expand Down Expand Up @@ -40,10 +41,8 @@ - (id)initWithFrame:(CGRect)frame{

- (void)drawRect:(CGRect)rect{

CGFloat toolbarSize = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 0 : 54;

CGFloat width = CGRectGetWidth(self.frame);
CGFloat height = CGRectGetHeight(self.frame) - toolbarSize;
CGFloat height = CGRectGetHeight(self.frame);

CGFloat heightSpan = floor(height / 2 - self.cropSize.height / 2);
CGFloat widthSpan = floor(width / 2 - self.cropSize.width / 2);
Expand All @@ -54,23 +53,14 @@ - (void)drawRect:(CGRect)rect{

//fill inner border
[[UIColor colorWithRed:1. green:1. blue:1. alpha:0.5] set];
UIRectFrame(CGRectMake(widthSpan - 2, heightSpan - 2, self.cropSize.width + 4, self.cropSize.height + 4));
// UIRectFrame(CGRectMake(widthSpan - 2, heightSpan - 2, self.cropSize.width + 4, self.cropSize.height + 4));
UIRectFrame(CGRectMake(widthSpan, heightSpan, self.cropSize.width, self.cropSize.height));

//fill inner rect
[[UIColor clearColor] set];
UIRectFill(CGRectMake(widthSpan, heightSpan, self.cropSize.width, self.cropSize.height));

// UIRectFill(CGRectMake(widthSpan + 2, heightSpan + 2, self.cropSize.width - 4, self.cropSize.height - 4));
UIRectFill(CGRectMake(widthSpan + 1, heightSpan + 1, self.cropSize.width - 2, self.cropSize.height - 2));


if (heightSpan > 30 && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)) {

[[UIColor whiteColor] set];
[NSLocalizedString(@"GKImoveAndScale", @"") drawInRect:CGRectMake(10, (height - heightSpan) + (heightSpan / 2 - 20 / 2) , width - 20, 20)
withFont:[UIFont boldSystemFontOfSize:20]
lineBreakMode:NSLineBreakByTruncatingTail
alignment:NSTextAlignmentCenter];

}
}

@end
Expand Down
3 changes: 3 additions & 0 deletions GKClasses/GKImageCropView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#import <UIKit/UIKit.h>

#define TOOLBAR_HEIGHT 72.f
#define TOOLBAR_PADDING 10.f

@interface GKImageCropView : UIView

@property (nonatomic, strong) UIImage *imageToCrop;
Expand Down
48 changes: 21 additions & 27 deletions GKClasses/GKImageCropView.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ - (void)layoutSubviews{
frameToCenter.origin.y = 0;

zoomView.frame = frameToCenter;
// zoomView.frame = CGRectMake(0, 0, 200, 200);
}

@end
Expand Down Expand Up @@ -131,15 +132,8 @@ -(CGRect)_calcVisibleRectForCropArea{
CGFloat scaleHeight = self.imageToCrop.size.height / self.cropSize.height;
CGFloat scale = 0.0f;

if (self.cropSize.width > self.cropSize.height) {
scale = (self.imageToCrop.size.width < self.imageToCrop.size.height ?
MAX(scaleWidth, scaleHeight) :
MIN(scaleWidth, scaleHeight));
}else{
scale = (self.imageToCrop.size.width < self.imageToCrop.size.height ?
MIN(scaleWidth, scaleHeight) :
MAX(scaleWidth, scaleHeight));
}
scale = MIN(scaleWidth, scaleHeight);

//extract visible rect from scrollview and scale it
CGRect visibleRect = [scrollView convertRect:scrollView.bounds toView:imageView];
return visibleRect = GKScaleRect(visibleRect, scale);
Expand Down Expand Up @@ -229,34 +223,34 @@ - (void)layoutSubviews{
[super layoutSubviews];

CGSize size = self.cropSize;
CGFloat toolbarSize = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 0 : 54;
self.xOffset = floor((CGRectGetWidth(self.bounds) - size.width) * 0.5);
self.yOffset = floor((CGRectGetHeight(self.bounds) - toolbarSize - size.height) * 0.5); //fixed
self.yOffset = floor((CGRectGetHeight(self.bounds) - size.height) * 0.5); //fixed

CGFloat height = self.imageToCrop.size.height;
CGFloat width = self.imageToCrop.size.width;

CGFloat faktor = 0.f;
CGFloat faktoredHeight = 0.f;
CGFloat faktoredWidth = 0.f;
CGFloat factor = 0.f, widthFactor = 0.f, heightFactor = 0.f;
CGFloat factoredHeight = 0.f;
CGFloat factoredWidth = 0.f;

if(width > height){

faktor = width / size.width;
faktoredWidth = size.width;
faktoredHeight = height / faktor;

} else {

faktor = height / size.height;
faktoredWidth = width / faktor;
faktoredHeight = size.height;
}
widthFactor = width / size.width;
heightFactor = height / size.height;
factor = MIN(widthFactor, heightFactor);
factoredWidth = width / factor;
factoredHeight = height / factor;

self.cropOverlayView.frame = self.bounds;
self.scrollView.frame = CGRectMake(xOffset, yOffset, size.width, size.height);
self.scrollView.contentSize = CGSizeMake(size.width, size.height);
self.imageView.frame = CGRectMake(0, floor((size.height - faktoredHeight) * 0.5), faktoredWidth, faktoredHeight);
self.imageView.frame = CGRectMake(0, floor((size.height - factoredHeight) * 0.5), factoredWidth, factoredHeight);

/* TODO
implement a feature that allows restricting the zoom scale to the max available
(based on image's resolution), to prevent pixelation. We simply have to deteremine the
max zoom scale and place it here
*/
[self.scrollView setContentOffset:CGPointMake((factoredWidth - size.width) * 0.5, (factoredHeight - size.height) * 0.5)];
[self.scrollView setZoomScale:1.0];
}

#pragma mark -
Expand Down
154 changes: 85 additions & 69 deletions GKClasses/GKImageCropViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#import "GKImageCropViewController.h"
#import "GKImageCropView.h"

#define HORIZONTAL_TEXT_PADDING 13.f

@interface GKImageCropViewController ()

@property (nonatomic, strong) GKImageCropView *imageCropView;
@property (nonatomic, strong) UIToolbar *toolbar;
@property (nonatomic, strong) UIView *toolbarView;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *useButton;

Expand All @@ -30,7 +32,7 @@ @implementation GKImageCropViewController

@synthesize sourceImage, cropSize, delegate;
@synthesize imageCropView;
@synthesize toolbar;
@synthesize toolbarView;
@synthesize cancelButton, useButton, resizeableCropArea;

#pragma mark -
Expand All @@ -49,111 +51,118 @@ - (void)_actionUse{


- (void)_setupNavigationBar{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(_actionCancel)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"GKIuse", @"")
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Use", @"Use")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(_actionUse)];
}


#pragma mark -
#pragma mark - Crop Rect Normalizing

- (CGSize)normalizedCropSizeForRect:(CGRect)rect
{
CGSize normalizedSize;
CGSize maxSize = CGSizeMake(rect.size.width - (2 * TOOLBAR_PADDING),
rect.size.height - (2. * (TOOLBAR_HEIGHT + TOOLBAR_PADDING)));
if (self.cropSize.height / self.cropSize.width > maxSize.height / maxSize.width) {
normalizedSize = CGSizeMake(self.cropSize.width * maxSize.height / self.cropSize.height,
maxSize.height);
} else {
normalizedSize = CGSizeMake(maxSize.width,
self.cropSize.height * maxSize.width / self.cropSize.width);
}
return normalizedSize;
}

- (void)_setupCropView{

self.imageCropView = [[GKImageCropView alloc] initWithFrame:self.view.bounds];
[self.imageCropView setImageToCrop:sourceImage];
[self.imageCropView setResizableCropArea:self.resizeableCropArea];
[self.imageCropView setCropSize:cropSize];
[self.imageCropView setCropSize:[self normalizedCropSizeForRect:self.view.bounds]];
self.imageCropView.clipsToBounds = YES;
[self.view addSubview:self.imageCropView];
}

- (CGSize)sizeForString:(NSString *)string withFont:(UIFont *)font{
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
attributes[NSFontAttributeName] = font;

CGSize constrainedSize = CGSizeMake(self.view.frame.size.width, TOOLBAR_HEIGHT);
CGSize neededSize = CGSizeMake(0, 0);

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
neededSize = [string boundingRectWithSize:constrainedSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil].size;
#else
neededSize = [string sizeWithFont:font
constrainedToSize:constrainedSize
lineBreakMode:NSLineBreakByTruncatingMiddle];
#endif
return CGSizeMake(neededSize.width, TOOLBAR_HEIGHT);
}

- (UIFont *)buttonFont
{
return [UIFont systemFontOfSize:18.f];
}

- (void)_setupCancelButton{
CGSize buttonSize = [self sizeForString:NSLocalizedString(@"Cancel", @"Cancel")
withFont:[self buttonFont]];

self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];

[self.cancelButton setBackgroundImage:[[UIImage imageNamed:@"PLCameraSheetButton.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateNormal];
[self.cancelButton setBackgroundImage:[[UIImage imageNamed:@"PLCameraSheetButtonPressed.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateHighlighted];

[[self.cancelButton titleLabel] setFont:[UIFont boldSystemFontOfSize:11]];
[[self.cancelButton titleLabel] setShadowOffset:CGSizeMake(0, 1)];
[self.cancelButton setFrame:CGRectMake(0, 0, 50, 30)];
[self.cancelButton setTitle:NSLocalizedString(@"GKIcancel",@"") forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor colorWithRed:0.173 green:0.176 blue:0.176 alpha:1] forState:UIControlStateNormal];
[self.cancelButton setTitleShadowColor:[UIColor colorWithRed:0.827 green:0.831 blue:0.839 alpha:1] forState:UIControlStateNormal];
[[self.cancelButton titleLabel] setFont:[self buttonFont]];
[self.cancelButton setFrame:CGRectMake(HORIZONTAL_TEXT_PADDING, 0, buttonSize.width, buttonSize.height)];
[self.cancelButton setTitle:NSLocalizedString(@"Cancel", @"Cancel") forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.cancelButton addTarget:self action:@selector(_actionCancel) forControlEvents:UIControlEventTouchUpInside];

}

- (void)_setupUseButton{
CGSize buttonSize = [self sizeForString:NSLocalizedString(@"Use",@"Use")
withFont:[self buttonFont]];

self.useButton = [UIButton buttonWithType:UIButtonTypeCustom];

[self.useButton setBackgroundImage:[[UIImage imageNamed:@"PLCameraSheetDoneButton.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateNormal];
[self.useButton setBackgroundImage:[[UIImage imageNamed:@"PLCameraSheetDoneButtonPressed.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateHighlighted];

[[self.useButton titleLabel] setFont:[UIFont boldSystemFontOfSize:11]];
[[self.useButton titleLabel] setShadowOffset:CGSizeMake(0, -1)];
[self.useButton setFrame:CGRectMake(0, 0, 50, 30)];
[self.useButton setTitle:NSLocalizedString(@"GKIuse",@"") forState:UIControlStateNormal];
[self.useButton setTitleShadowColor:[UIColor colorWithRed:0.118 green:0.247 blue:0.455 alpha:1] forState:UIControlStateNormal];
[[self.useButton titleLabel] setFont:[self buttonFont]];
[self.useButton setFrame:CGRectMake(self.view.frame.size.width - (buttonSize.width + HORIZONTAL_TEXT_PADDING), 0, buttonSize.width, buttonSize.height)];
[self.useButton setTitle:NSLocalizedString(@"Use",@"Use") forState:UIControlStateNormal];
[self.useButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.useButton addTarget:self action:@selector(_actionUse) forControlEvents:UIControlEventTouchUpInside];

}

- (UIImage *)_toolbarBackgroundImage{

CGFloat components[] = {
1., 1., 1., 1.,
123./255., 125/255., 132./255., 1.
};

UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 54), YES, 0.0);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);

CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0, 0), CGPointMake(0, 54), kCGImageAlphaNoneSkipFirst);

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();

return viewImage;
}

- (void)_setupToolbar{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectZero];
[self.toolbar setBackgroundImage:[self _toolbarBackgroundImage] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[self.view addSubview:self.toolbar];
self.toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0,
self.view.frame.size.height - TOOLBAR_HEIGHT,
self.view.frame.size.width,
TOOLBAR_HEIGHT)];
self.toolbarView.backgroundColor = [UIColor colorWithRed:20./255. green:20./255. blue:20./255. alpha:0.65];
[self.view addSubview:self.toolbarView];

[self _setupCancelButton];
[self _setupUseButton];

UILabel *info = [[UILabel alloc] initWithFrame:CGRectZero];
info.text = NSLocalizedString(@"GKImoveAndScale", @"");
info.textColor = [UIColor colorWithRed:0.173 green:0.173 blue:0.173 alpha:1];
info.backgroundColor = [UIColor clearColor];
info.shadowColor = [UIColor colorWithRed:0.827 green:0.831 blue:0.839 alpha:1];
info.shadowOffset = CGSizeMake(0, 1);
info.font = [UIFont boldSystemFontOfSize:18];
[info sizeToFit];

UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithCustomView:self.cancelButton];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *lbl = [[UIBarButtonItem alloc] initWithCustomView:info];
UIBarButtonItem *use = [[UIBarButtonItem alloc] initWithCustomView:self.useButton];

[self.toolbar setItems:[NSArray arrayWithObjects:cancel, flex, lbl, flex, use, nil]];
[self.toolbarView addSubview:self.cancelButton];
[self.toolbarView addSubview:self.useButton];
}
}

- (BOOL)prefersStatusBarHidden {
return YES;
}

#pragma mark -
#pragma Super Class Methods

Expand All @@ -169,7 +178,8 @@ - (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.

self.title = NSLocalizedString(@"GKIchoosePhoto", @"");
self.title = NSLocalizedString(@"Choose Photo", @"Choose Photo");
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

[self _setupNavigationBar];
[self _setupCropView];
Expand All @@ -187,11 +197,17 @@ - (void)viewDidUnload{
// Release any retained subviews of the main view.
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}

- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];

self.imageCropView.frame = self.view.bounds;
self.toolbar.frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 54, 320, 54);
self.toolbarView.frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - TOOLBAR_HEIGHT, self.view.frame.size.width, TOOLBAR_HEIGHT);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Expand Down
3 changes: 2 additions & 1 deletion GKClasses/GKImagePicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

@property (nonatomic, weak) id<GKImagePickerDelegate> delegate;
@property (nonatomic, assign) CGSize cropSize; //default value is 320x320 (which is exactly the same as the normal imagepicker uses)
@property (nonatomic, strong, readonly) UIImagePickerController *imagePickerController;
@property (nonatomic, assign) BOOL resizeableCropArea;

- (void)showActionSheetOnViewController:(UIViewController *)viewController onPopoverFromView:(UIView *)popoverView;

@end


Expand Down
Loading