gekitz / gkimagepicker Goto Github PK
View Code? Open in Web Editor NEWEnables a custom crop rect for UIImagePickerController
License: MIT License
Enables a custom crop rect for UIImagePickerController
License: MIT License
I hope that the image will fill the crop frame.
As the image quality of the custom crop was improved with the latest commit 2a91f31 we should definitely improve the quality of the "Resizeable Custom Crop" too.
@Pathonhauser, any ideas on that?
Hi,
There is an issue on iOS8 that will stall the camera when the user cancels out of the crop-area.
Repro Steps:
I suspect it may have to do with the way iOS8 handles action sheets (?), and the behaviour of dismissing the CropImageViewController. I'm currently investigating it.
Anyone bumped into this, or fixed it?
I just noticed that right after I select an image from my camera roll and the crop viewController is opened I get a big memory leak. Is this a known issue or am I doing something wrong?
Edit: After some digging I believe this is the problematic code under GKImageCropViewController:
(UIImage *)_toolbarBackgroundImage{
..
..
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0, 0), CGPointMake(0, 54), kCGImageAlphaNoneSkipFirst);
..
}
After commenting out those lines I lost my toolbar background Image (of course) but I also lost my leaks. And advice would be great.
Hi, i noticed in some cases scaling is wrong. Example(resizeableCropArea=NO):
Image size: 200x100
Crop size: 120x110
I also noticed that it works OK when resizableCropArea=YES due to different scaling calculation. Using this seems to solve all problems for me, including square images, tested on 20+ images:
-(CGRect)_calcVisibleRectForCropArea
{
CGFloat scale = self.imageView.image.size.width / self.imageView.frame.size.width;
scale *= self.scrollView.zoomScale;
//extract visible rect from scrollview and scale it
CGRect visibleRect = [scrollView convertRect:scrollView.bounds toView:imageView];
visibleRect = GKScaleRect(visibleRect, scale);
return visibleRect;
}
Note: I always set images to normal orientation before passing them to GKImageCropViewController, otherwise it doesn't work.
BTW: Navigation bar sometimes disappears :P
This problem is caused because the scale is not always a whole number in the croppedImage function.
When the rect is scaled up, non full pixels are often created. When the CGImageCreateWithImageInRect cropping function crops the image, it rounds those non full pixels up!
This can be solved by changing this
static CGRect GKScaleRect(CGRect rect, CGFloat scale)
{
return CGRectMake(rect.origin.x * scale, rect.origin.y * scale, rect.size.width * scale, rect.size.height * scale);
}
to this
static CGRect GKScaleRect(CGRect rect, CGFloat scale)
{
return CGRectMake(floor(rect.origin.x * scale), floor(rect.origin.y * scale), floor(rect.size.width * scale), floor(rect.size.height * scale));
}
HI,
Great program. Thanks
on the resizable custom crop function the initial placement of the cropview and all the handles is correct. The handles along the vertical are only half circles as the image covers most of the popover view.
When a (any) handle is moved the cropview reduces in size, and all handles become full circles. The cropview seems to back away from the popover view boarder, and now covers part of the image. It is now impossible to get back to the initial position of the handles. There seems to be a constrain along the boarder of the popover view preventing the handles back to the initial condition.Is this correct and is this constrain adjustable.?
You can easily reproduce this on the demo program with the attached image.
Rashid
[email protected]
There are a memory warning. Why appear this? I guess appear this because of the ipod touch 5 's memory is too small and the album have too many big photos.
Hi,
I want to reduce the zooming of the image. is it possible? please give suggestions.
Thank you
thanks for the library. However, the quality of the cropped image is very low (custom & resizable crop). If it helps, i'm using the cropped image for OCR using Tesseract. Upon The cropped image TOCR gives some vague symbols and just 1or 2 digits. If the apple's default picker is used, the OCR works perfectly.
Can you provide any help? Thanks.
Edit1: Forgot to mention, I modified the GKImagePicker.m to capture a new photo instead of choosing from library.
ps:
I'm using the latest version of library. I already read the issues #7, #11, #12, #32.
When I set up the app to use the camera instead of selecting from photo library, the status bar disappears when the camera view shows, and never reappears.
Hello! this problem happens when the crop size is not a square, like for example 320x80.
Any suggestions?
Thanks!
Hey there,
first of all, very nice work!! Your custom cropview for UIImagePicker was excactly what i was searching for!
I tested it in iOS5+ and everything works just fine!
But when i run your GKImagePicker on iOS4.3 in iPad simulator, all items are placed in the lower right corner of the popover. I figured out, that you use "self.view.bounds" for any size depending calculations. But this seem not be set properly in iOS4.3 when setting the "contentSizeForViewInPopover" property. By setting the view bounds properly everything is shown perfect, but i'm impossible to scale and move the image.
Can you confirm this issue? It would be nice if you could fix that.
kind regards
in file GKImageCropView.m
in method _calcVisibleRectForCropArea
L134 change to " if (self.cropSize.width >= self.cropSize.height) "
If my iPhone app runs in the emulator mode iPad i got an error
'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 240]'
width/faktoredWidth was devided by zero
Is there a possibility to use iphone only image picking on a ipad running the iphone app emulator?
I think i have to test this idom.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
[[[UIDevice currentDevice] model] hasPrefix:@"iPad"]) {
// This app is an iPhone app running on an iPad
}
If i remove all ipad idoms i still got this error.
When using the custom crop, the image is not cropped to the dimensions of the crop zone. The apple crop and resizable crop work just fine.
Just found a major bug
I just cropped a big image of a text document and the text is unreadable. If I go to my camera roll and zoom in on the text it's crisp and clear.
I've found out that a 2000x2000 image is now 600x500. (and the size of it haven't changed, I only cropped one line of pixels)
You can repredouce the bug by setting the crop points in Resizable Custom crop on a ZOOMED-OUT picture (this is where the bug is) If you'll zoom-in on the picture and then crop it it will be fine.
Thanks
I'm just running the example application included in the project. When selecting a panorama in the Apple's Default Cropper, Memory usage is 15.5 MB. When using the Custom Cropper, it jumps to 88.6. This is using a 7.4 MB JPG, 8208 x 2332 image. I've done a lot of testing with other panoramas as well, it gets worse the larger the image is.
Within the context of another app, this is causing memory warnings, and in some cases, crashing the app.
Baseline:
Default:
Custom Crop:
VM Allocations is pointing to ImageIO as the culprit, but I haven't found a potential solution.
Crashes when I try to implement it in iOS 7
Is there any way to set maximum zoom of the taken picture ?. I don't understand when I tried to set image to UIButton and the picture is really small because I zoomed in to maximum, it's not really fill the size of button, there is a blank space in everyside of button.
Seems like when you take an image with the camera, you cannot move it (pan it) immediately. If you zoom in a bit, the image is then able to be panned. I think this is tied to an issue with the content size, I corrected by using the faktored width and height.
In addition there was an issue where the image was positioned not exactly in the correct spot, fixed by setting the contentOffset.
in GKImageCropView.m
(void)layoutSubviews{
//some code here...
self.cropOverlayView.frame = self.bounds;
//JJE - explicitly specify the content offset here to ensure there isn't a gap.
self.scrollView.contentOffset = CGPointMake(0,0);
self.scrollView.frame = CGRectMake(xOffset, yOffset, size.width, size.height);
//JJE - changed the contentsize to the new width and height so we can move it immediately
self.scrollView.contentSize = CGSizeMake(faktoredWidth, faktoredHeight);
self.imageView.frame = CGRectMake(0, floor((size.height - faktoredHeight) * 0.5), faktoredWidth, faktoredHeight);
}
I've got some other custom mods to this I can share.
Wouldn't it be great if the user could scale/change the cropRectangle?
Currently the default setting is Aspect-Fit to display the photo in the Crop viewer. I want this to be zoomed in, so that would be Aspect-Fill. However changing the setting for this is not working:
GKImageCropView.m
Change
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
to AspectFill in the initWithFrame method:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor blackColor];
self.scrollView = [[ScrollView alloc] initWithFrame:self.bounds ];
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
self.scrollView.decelerationRate = 0.0;
self.scrollView.backgroundColor = [UIColor clearColor];
[self addSubview:self.scrollView];
self.imageView = [[UIImageView alloc] initWithFrame:self.scrollView.frame];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:self.imageView];
self.scrollView.minimumZoomScale = CGRectGetWidth(self.scrollView.frame) / CGRectGetWidth(self.imageView.frame);
self.scrollView.maximumZoomScale = 20.0;
[self.scrollView setZoomScale:1.0];
}
return self;
}
Can anyone point me in the right direction?
Hi,
I have created a new single view app. I dragged your files into the project and built it.
I get 12 error (see images below).
When running the demo app, there are no issues.
I have removed only GKImagePicker.m/h from my project and there are NO issues.
Any idea why this is?
EDIT:
needed to import UIKit
When loading the zoom and scale view you will have to zoom first before you can move the image.
This can be fixed if we set the contentsize of the scrollview larger than de cropsize.
GKImageCropView.m
change:
self.scrollView.contentSize = CGSizeMake(size.width, size.height);
in:
self.scrollView.contentSize = CGSizeMake(width, height);
First of all this is the great code you have written.
I have one issue with cropping image with portrait mode landscape will work fine.
I have change the code to fetch image from library to camera, when I capture image as a landscape mode with camera it will work perfectly, but when I was capture image as portrait image, image shows in corp box as aspect to fit, my image crop box size is 300x 200 means landscape box, in landscape box portrait image not feet well.
First image is the current scenario. second image is what I am trying to say.
Because the reason behind If i am crop like a first image it still give (Output result after crop ) me the portrait image not landscape image...
I've been using GKImagePicker for a while now and I was wondering, is there a way I can take a picture to cropping right from the camera? Thanks
GKImagePicker returns incorrectly cropped image for squared images (height == width). And I have found a solution, you need to replace in GKImageCropView.m this code:
" 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));
}"
with this one ("<" replaced with "<="):
" 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));
}"
sorry for my english :)
I don't know why it doesn't show resizable area for camera picked photos. I had resizeableCropArea = YES too.
Hi,
First, I would like to say that you did an awesome job with this component.
Running the test project, I am selecting an image from my camera roll that is:
1,856,278 bytes (1.9 MB on disk)
Running through all 3 options (Custom Crop, Normal Crop, and Resizable Custom Crop) and not cropping the image in anyway, I am capturing the size of the image when I select the Use button.
Custom Crop -> 12,210,978 bytes
Normal Crop -> 330,878 bytes
Resizable Custom Crop -> 287,723 bytes
I have the latest code and I am wondering if you could help me out here. I am working on a project where the user can crop an image and then I upload that image. When I upload, the size of the image is drastically reduced. Any help would be appreciated.
Thanks and keep up the good work.
ie.
This is useful when integrating the image picker with an existing environment that crops server side.
hi,
I am using the GKImagePicker in my app.i have to display a image with width and height 320 * 320 . I keep the crop size as 320 *320. for some while little zooming of image i am getting the exact image while deep zooming , the image in the crop section is not displaying totally. only top of the image is displaying. please give suggestions for getting exact crop section of the image .in iPhone6 and 6 + exact cropping is occurring.
Please give suggestions
Thank you
i want to localize the strings like 'choose photo', 'move and scale','use' , and i use the method of - (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName but i don't know the tableName, please help!
There are some deprecated methods/enums that can be fixed easily.
GKClasses/GKImageCropOverlayView.m
line 70: Change UILineBreakModeTailTruncation to NSLineBreakByTruncatingTail
line 71: Change UITextAlignmentCenter to NSTextAlignmentCenter
GKClasses/GKImageCropViewController.m
line 184: Remove viewDidUnload method, never gets called with ARC
Hi, I'm trying to use this amazing custom crop picker in my landscape only iPad app. I think the problem is my custom crop size is CGSizeMake(370, 550) which is bigger than the UIImagePicker default size in iPad landscape. And i tried for 2 days now but there doesn't seem a way to make the picker bigger.
do you have the same code in swift? thanks
This happens in the sample app the same as it does in my app.
Simply alloc-init'ing and presenting the view controller takes about 1 second, with the main thread blocked in the mean time. It's pretty clunky.
It wouldn't be too difficult to just send it off on a dispatch_async() and call back to the main thread when ready- but I think this should be covered GKImagePicker's implementation.
I'll test it in instruments and post info if I have time tomorrow.
CGFloat imageAspectRatio = width / height;
CGFloat cropAspectRatio = size.width / size.height;
// if(width > height){
if (imageAspectRatio > cropAspectRatio) {
faktor = width / size.width;
faktoredWidth = size.width;
faktoredHeight = height / faktor;
} else {
faktor = height / size.height;
faktoredWidth = width / faktor;
faktoredHeight = size.height;
}
Hi,
How to display the image with crop size after selecting the image from gallery.
Steps to reproduce:
The screens below display both flows: first a correct crop, then the misplaced one.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.