Set dimensions for UIImagePickerController "move and scale" cropbox
Solution 1
Not possible with UIImagePickerController unfortunately. The solution I recommend is to disable editing for the image picker and handle it yourself. For instance, I put the image in a scrollable, zoomable image view. On top of the image view is a fixed position "crop guide view" that draws the crop indicator the user sees. Assuming the guide view has properties for the visible rect (the part to keep) and edge widths (the part to discard) you can get the cropping rectangle like so. You can use the UIImage+Resize category to do the actual cropping.
CGRect cropGuide = self.cropGuideView.visibleRect;
UIEdgeInsets edges = self.cropGuideView.edgeWidths;
CGPoint cropGuideOffset = self.cropScrollView.contentOffset;
CGPoint origin = CGPointMake( cropGuideOffset.x + edges.left, cropGuideOffset.y + edges.top );
CGSize size = cropGuide.size;
CGRect crop = { origin, size };
crop.origin.x = crop.origin.x / self.cropScrollView.zoomScale;
crop.origin.y = crop.origin.y / self.cropScrollView.zoomScale;
crop.size.width = crop.size.width / self.cropScrollView.zoomScale;
crop.size.height = crop.size.height / self.cropScrollView.zoomScale;
photo = [photo croppedImage:crop];
Solution 2
Kinda late to the game but I think this may be what you are looking for: https://github.com/gekitz/GKImagePicker
Solution 3
Here is a solution for manual cropping by Ming Yang. https://github.com/myang-git/iOS-Image-Crop-View
It offers a rectangular frame, which the user can slide or drag to fit the required portion of the image in the rectangle. Please note that this solution does the reverse of the question asked - lets the rectangle size vary, but eventually brings the desired result.
It is coded in Objective-C. You may have to either code it in Swift or simply build a bridging header to connect the Objective-C code with Swift code.
Solution 4
It's now later than late but may be useful for someone. This is the library I've used for swift (many thanks to Tim Oliver):
as described in README file in GitHub link above, by using this library you can get cropped images in user-defined rectangular and also in a circular mode, e.g. for updating profile image. below is sample code from GitHub:
func presentCropViewController {
let image: UIImage = ... //Load an image
let cropViewController = CropViewController(image: image)
cropViewController.delegate = self
present(cropViewController, animated: true, completion: nil)
}
func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
// 'image' is the newly cropped version of the original image
}
Thomas K
Updated on May 30, 2020Comments
-
Thomas K about 4 years
How does the "move and scale screen" determine dimensions for its cropbox?
Basically I would like to set a fixed width and height for the "CropRect" and let the user move and scale his image to fit in to that box as desired.
Does anyone know how to do this? (Or if it is even possible with the UIImagePickerController)
Thanks!
-
Pinwheeler about 11 yearsWell I here in future land certainly appreciated it!
-
Josh Bernfeld almost 11 yearsI love you, this just saved me so much time.
-
Admin almost 11 yearsCan you please provide the sample project or anything else please.
-
gdm over 10 yearsHow to use it for Camera ?
-
gdm over 10 yearsHow is it possible to make a bigger size for the imagepicker when it shows up?
-
ashack about 10 yearsIt's a bit buggy in iOS7, I would look for another solution (and am currently myself).
-
Varun Naharia over 8 yearsbetter then any other library I find.
-
ios over 8 yearsNot working in iOS 9 . stackoverflow.com/questions/35645425/…
-
Uma Madhavi over 7 years@Marc. How can u guide me how to perform using swift
-
sɐunıɔןɐqɐp about 6 yearsWelcome to Stack Overflow! While links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: How do I write a good answer?