Want a nice crop editor for your picture? Not satisfied with the one provided for Apple? Likes rounded images?
LECropPictureViewController
is the component for you :)! See the how works in the gif below:
Drag and copy all files in the LECropPictureViewController folder into your project, or add it as a git submodule.
LECropPictureViewController is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "LECropPictureViewController", "~> 0.1"
LECropPictureViewController
works with any image. You just have to provide the image and a cropType when creating a new instance, and then present the view controller. The possible cropTypes are:
LECropPictureTypeRounded
LECropPictureTypeRect
#import "LECropPictureViewController.h"
LECropPictureViewController *cropPictureController = [[LECropPictureViewController alloc] initWithImage:image andCropPictureType:LECropPictureTypeRounded];
[self presentViewController:cropPictureController animated:YES completion:nil];
The callback for the cropped picture is given through a block. See the exemple below, presenting the LECropPictureViewController
inside the delegate of a UIImagePickerController:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:NO completion:nil];
LECropPictureViewController *cropPictureController = [[LECropPictureViewController alloc] initWithImage:image andCropPictureType:LECropPictureTypeRounded];
cropPictureController.imageView.contentMode = UIViewContentModeScaleAspectFit;
cropPictureController.photoAcceptedBlock = ^(UIImage *croppedPicture){
self.imageView.image = croppedPicture;
};
[self presentViewController:cropPictureController animated:NO completion:nil];
}
The LECropPictureViewController
has public properties for it's components. With this, you can do things like changing the contentMode of the imageView, changing the text of the barButtonItems, etc.
@property (weak, nonatomic) UIBarButtonItem *cancelButtonItem;
@property (weak, nonatomic) UIBarButtonItem *acceptButtonItem;
@property (weak, nonatomic) UIImageView *imageView;
Besides the subviews, you can customize other things like the initial frame of the crop area, borderColor and borderWidth, like below:
(...)
LECropPictureViewController *cropPictureController = [[LECropPictureViewController alloc] initWithImage:image andCropPictureType:LECropPictureTypeRounded];
cropPictureController.cropFrame = CGRectMake(50, 50, 250, 250);
cropPictureController.borderColor = [UIColor grayColor];
cropPictureController.borderWidth = 1.0;
(...)
You can even implement the @property(copy) void(^photoRejectedBlock)();
, if you want to override the default behaviour of the cancel button (which only dismiss the controller). Be aware that, if you do this, you will be responsible to dismiss the controller yourself.
cropPictureController.photoRejectedBlock = ^{
NSLog(@"Doing something here!");
[self dismissViewControllerAnimated:YES completion:nil];
};
Liked the project? Is there something missing or that could be better? Feel free to contribute :)
-
Fork it
-
Create your branch
git checkout -b name-your-feature
-
Commit it
git commit -m 'the difference'
-
Push it
git push origin name-your-feature
-
Create a Pull Request
Lucas Eduardo, [email protected]
LECropPictureViewController is available under the MIT license. See the LICENSE file for more info.