Set dimensions for UIImagePickerController "move and scale" cropbox

Thomas K picture Thomas K · Jan 28, 2012 · Viewed 47.7k times · Source

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!

Answer

Marc Charbonneau picture Marc Charbonneau · Feb 1, 2012

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];