ios 为 UIImagePickerController“移动和缩放”裁剪框设置尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9041732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:32:19  来源:igfitidea点击:

Set dimensions for UIImagePickerController "move and scale" cropbox

ioscocoa-touchipaduiimagepickercontrollercrop

提问by Thomas K

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.

基本上我想为“CropRect”设置一个固定的宽度和高度,并让用户根据需要移动和缩放他的图像以适应该框。

Does anyone know how to do this? (Or if it is even possible with the UIImagePickerController)

有谁知道如何做到这一点?(或者甚至可以使用 UIImagePickerController)

Thanks!

谢谢!

采纳答案by Marc Charbonneau

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+Resizecategory to do the actual cropping.

不幸的是 UIImagePickerController 不可能。我推荐的解决方案是禁用图像选择器的编辑并自行处理。例如,我将图像放在可滚动、可缩放的图像视图中。在图像视图的顶部是一个固定位置的“裁剪指南视图”,它绘制了用户看到的裁剪指示器。假设向导视图具有可见矩形(要保留的部分)和边缘宽度(要丢弃的部分)的属性,您可以像这样获得裁剪矩形。您可以使用UIImage+Resize类别进行实际裁剪。

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

回答by Yuriy

Kinda late to the game but I think this may be what you are looking for: https://github.com/gekitz/GKImagePicker

有点晚了,但我认为这可能是您正在寻找的:https: //github.com/gekitz/GKImagePicker

回答by Sriram

Here is a solution for manual cropping by Ming Yang. https://github.com/myang-git/iOS-Image-Crop-View

这是杨明手动裁剪的解决方案。 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.

它是用 Objective-C 编码的。您可能必须使用 Swift 对其进行编码,或者只是构建一个桥接头来将 Objective-C 代码与 Swift 代码连接起来。

回答by MFA

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):

现在为时已晚,但可能对某人有用。这是我用于 swift 的库(非常感谢Tim Oliver):

TOCropViewController

TOCropViewController

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:

如上面 GitHub 链接中的 README 文件所述,通过使用此库,您可以获得用户定义的矩形和圆形模式的裁剪图像,例如用于更新个人资料图像。下面是来自 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
    }