Skip to content

Commit

Permalink
Merge pull request #12 from giladno/master
Browse files Browse the repository at this point in the history
do not pop controller automatically when delegate is set
  • Loading branch information
itouch2 committed Apr 10, 2015
2 parents db28c89 + 32cb9ea commit c3fbf49
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
4 changes: 2 additions & 2 deletions PhotoTweaks/PhotoTweaks/PhotoTweaksViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@
/**
Called on image cropped.
*/
- (void)finishWithCroppedImage:(UIImage *)croppedImage;
- (void)photoTweaksController:(PhotoTweaksViewController *)controller didFinishWithCroppedImage:(UIImage *)croppedImage;

@end
@end
60 changes: 31 additions & 29 deletions PhotoTweaks/PhotoTweaks/PhotoTweaksViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ - (instancetype)initWithImage:(UIImage *)image
- (void)viewDidLoad
{
[super viewDidLoad];

self.navigationController.navigationBarHidden = YES;

if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}

self.view.clipsToBounds = YES;
self.view.backgroundColor = [UIColor photoTweakCanvasBackgroundColor];

[self setupSubviews];
}

Expand All @@ -49,7 +49,7 @@ - (void)setupSubviews
self.photoView = [[PhotoTweakView alloc] initWithFrame:self.view.bounds image:self.image];
self.photoView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.photoView];

UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBtn.frame = CGRectMake(8, CGRectGetHeight(self.view.frame) - 40, 60, 40);
cancelBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
Expand All @@ -63,15 +63,15 @@ - (void)setupSubviews
cancelBtn.titleLabel.font = [UIFont systemFontOfSize:17];
[cancelBtn addTarget:self action:@selector(cancelBtnTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelBtn];

UIButton *cropBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cropBtn.titleLabel.textAlignment = NSTextAlignmentRight;
cropBtn.frame = CGRectMake(CGRectGetWidth(self.view.frame) - 60, CGRectGetHeight(self.view.frame) - 40, 60, 40);
[cropBtn setTitle:NSLocalizedStringFromTable(@"Done", @"PhotoTweaks", nil) forState:UIControlStateNormal];
UIColor *saveButtonTitleColor = !self.saveButtonTitleColor ?
[UIColor saveButtonColor] : self.saveButtonTitleColor;
[cropBtn setTitleColor:saveButtonTitleColor forState:UIControlStateNormal];

UIColor *saveButtonHighlightTitleColor = !self.saveButtonHighlightTitleColor ?
[UIColor saveButtonHighlightedColor] : self.saveButtonHighlightTitleColor;
[cropBtn setTitleColor:saveButtonHighlightTitleColor forState:UIControlStateHighlighted];
Expand All @@ -88,33 +88,31 @@ - (void)cancelBtnTapped
- (void)saveBtnTapped
{
CGAffineTransform transform = CGAffineTransformIdentity;

// translate
CGPoint translation = [self.photoView photoTranslation];
transform = CGAffineTransformTranslate(transform, translation.x, translation.y);

// rotate
transform = CGAffineTransformRotate(transform, self.photoView.angle);

// scale
CGAffineTransform t = self.photoView.photoContentView.transform;
CGFloat xScale = sqrt(t.a * t.a + t.c * t.c);
CGFloat yScale = sqrt(t.b * t.b + t.d * t.d);
transform = CGAffineTransformScale(transform, xScale, yScale);

CGImageRef imageRef = [self newTransformedImage:transform
sourceImage:self.image.CGImage
sourceSize:self.image.size
sourceOrientation:self.image.imageOrientation
outputWidth:self.image.size.width
cropSize:self.photoView.cropView.frame.size
imageViewSize:self.photoView.photoContentView.bounds.size];

UIImage *image = [UIImage imageWithCGImage:imageRef];
if ([self.delegate respondsToSelector:@selector(finishWithCroppedImage:)]) {
[self.delegate finishWithCroppedImage:image];
}

CGImageRelease(imageRef);

if (self.autoSaveToLibray) {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
Expand All @@ -123,16 +121,20 @@ - (void)saveBtnTapped
}];
} else {
}

CGImageRelease(imageRef);

if ([self.delegate respondsToSelector:@selector(photoTweaksController:didFinishWithCroppedImage:)]) {
[self.delegate photoTweaksController:self didFinishWithCroppedImage:image];
return;
}

[self.navigationController popViewControllerAnimated:YES];
}

- (CGImageRef)newScaledImage:(CGImageRef)source withOrientation:(UIImageOrientation)orientation toSize:(CGSize)size withQuality:(CGInterpolationQuality)quality
{
CGSize srcSize = size;
CGFloat rotation = 0.0;

switch(orientation)
{
case UIImageOrientationUp: {
Expand All @@ -152,7 +154,7 @@ - (CGImageRef)newScaledImage:(CGImageRef)source withOrientation:(UIImageOrientat
default:
break;
}

CGContextRef context = CGBitmapContextCreate(NULL,
size.width,
size.height,
Expand All @@ -161,20 +163,20 @@ - (CGImageRef)newScaledImage:(CGImageRef)source withOrientation:(UIImageOrientat
CGImageGetColorSpace(source),
(CGBitmapInfo)kCGImageAlphaNoneSkipFirst //CGImageGetBitmapInfo(source)
);

CGContextSetInterpolationQuality(context, quality);
CGContextTranslateCTM(context, size.width/2, size.height/2);
CGContextRotateCTM(context,rotation);

CGContextDrawImage(context, CGRectMake(-srcSize.width/2 ,
-srcSize.height/2,
srcSize.width,
srcSize.height),
source);

CGImageRef resultRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);

return resultRef;
}

Expand All @@ -190,10 +192,10 @@ - (CGImageRef)newTransformedImage:(CGAffineTransform)transform
withOrientation:sourceOrientation
toSize:sourceSize
withQuality:kCGInterpolationNone];

CGFloat aspect = cropSize.height/cropSize.width;
CGSize outputSize = CGSizeMake(outputWidth, outputWidth*aspect);

CGContextRef context = CGBitmapContextCreate(NULL,
outputSize.width,
outputSize.height,
Expand All @@ -203,22 +205,22 @@ - (CGImageRef)newTransformedImage:(CGAffineTransform)transform
CGImageGetBitmapInfo(source));
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, outputSize.width, outputSize.height));

CGAffineTransform uiCoords = CGAffineTransformMakeScale(outputSize.width / cropSize.width,
outputSize.height / cropSize.height);
uiCoords = CGAffineTransformTranslate(uiCoords, cropSize.width/2.0, cropSize.height / 2.0);
uiCoords = CGAffineTransformScale(uiCoords, 1.0, -1.0);
CGContextConcatCTM(context, uiCoords);

CGContextConcatCTM(context, transform);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context, CGRectMake(-imageViewSize.width/2.0,
-imageViewSize.height/2.0,
imageViewSize.width,
imageViewSize.height)
, source);

CGImageRef resultRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGImageRelease(source);
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PhotoTweaks is an interface to crop photos. It can let user drag, rotate, scale

PhotoTweaksViewController offers all the operations to crop the photo, which includes translation, rotate and scale..

To use it,
To use it,

```objective-c
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
Expand All @@ -24,8 +24,9 @@ photoTweaksViewController.delegate = self;
```
Get the cropped image
```objective-c
- (void)finishWithCroppedImage:(UIImage *)croppedImage
- (void)photoTweaksController:(PhotoTweaksViewController *)controller didFinishWithCroppedImage:(UIImage *)croppedImage
{
[controller.navigationController dismissViewControllerAnimated:YES completion:nil];
// cropped image
}
```
Expand Down

0 comments on commit c3fbf49

Please sign in to comment.