Created
April 21, 2012 13:51
-
-
Save barrettj/2437165 to your computer and use it in GitHub Desktop.
Example of using BJImageCropper
This file contains 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
- (void)displayCrop { | |
ImageCropViewController *crop = [[ImageCropViewController alloc] init]; | |
NSMutableDictionary *imageInfo = [self getImageInfo:currentImage]; | |
crop.image = [UIImage imageWithContentsOfFile:[imageInfo getStringSetting:@"Location" withDefault:@""]]; | |
crop.cropSet = ^(CGRect newCrop, ImageCropViewController* imageCropView) { | |
NSMutableDictionary *imageInfo = [self getImageInfo:currentImage]; | |
[imageInfo saveRectSetting:@"Crop" withValue:newCrop]; | |
if (self.isViewLoaded) { | |
[self addImageWithInfo:imageInfo andIndex:currentImage]; | |
} | |
[self hideModalViewControllerAnimated:YES]; | |
}; | |
crop.cropCancel = ^(ImageCropViewController* imageCropView) { | |
[self hideModalViewControllerAnimated:YES]; | |
}; | |
if ([[imageInfo allKeys] containsObject:@"Crop"]) { | |
crop.defaultCrop = [imageInfo getRectSetting:@"Crop"]; | |
} | |
crop.modalTransitionStyle = UIModalTransitionStyleCoverVertical; | |
[self showModalViewController:crop animated:YES]; | |
} |
This file contains 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
// | |
// ImageCropViewController.m | |
// | |
// Created by Barrett Jacobsen on 6/23/11. | |
// Copyright 2011 __MyCompanyName__. All rights reserved. | |
// | |
#import "ImageCropViewController.h" | |
#import <QuartzCore/QuartzCore.h> | |
@implementation ImageCropViewController | |
@synthesize image; | |
@synthesize cropSet; | |
@synthesize cropCancel; | |
@dynamic defaultCrop; | |
#pragma mark - View lifecycle | |
- (CGRect)defaultCrop { | |
return defaultCrop; | |
} | |
- (void)setDefaultCrop:(CGRect)newCrop { | |
hasSetDefaultCrop = YES; | |
defaultCrop = newCrop; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tactile_noise.png"]]; | |
imageCropper = [[BJImageCropper alloc] initWithImage:image andMaxSize:CGSizeMake(1024, 625)]; | |
[self.view addSubview:imageCropper]; | |
if (hasSetDefaultCrop) imageCropper.crop = self.defaultCrop; | |
imageCropper.center = self.view.center; | |
imageCropper.imageView.layer.shadowColor = [[UIColor blackColor] CGColor]; | |
imageCropper.imageView.layer.shadowRadius = 3.0f; | |
imageCropper.imageView.layer.shadowOpacity = 0.8f; | |
imageCropper.imageView.layer.shadowOffset = CGSizeMake(1, 1); | |
} | |
- (void)viewDidUnload { | |
[super viewDidUnload]; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | |
return UIInterfaceOrientationIsLandscape(interfaceOrientation); | |
} | |
- (IBAction)didPressDone:(id)sender { | |
if (self.cropSet != nil) { | |
self.cropSet(imageCropper.crop, self); | |
} | |
} | |
- (IBAction)didPressCancel:(id)sender { | |
if (self.cropCancel != nil) { | |
self.cropCancel(self); | |
} | |
} | |
+ (UIImage*) getImage:(UIImage*)image croppedToRect:(CGRect)rect { | |
UIGraphicsBeginImageContext(rect.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// translated rectangle for drawing sub image | |
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, image.size.width, image.size.height); | |
// clip to the bounds of the image context | |
// not strictly necessary as it will get clipped anyway? | |
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height)); | |
// draw image | |
[image drawInRect:drawRect]; | |
// grab image | |
UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return croppedImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment