Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望增加功能:从相册选图后 马上进入裁剪模式 且 设定裁剪尺寸 #945

Open
lovivid opened this issue Oct 28, 2024 · 1 comment

Comments

@lovivid
Copy link

lovivid commented Oct 28, 2024

Issue Description

希望增加1个功能:从相册选图后 马上进入裁剪模式 且 设定裁剪尺寸

Description and Steps

比如,当我需要一个正方形的图片,从相册选图后,无需手动点击裁剪按钮,而是自动进入裁剪模式 且 设定裁剪框为我需要正方形
比如,当我需要一个长方形的图片,从相册选图后,无需手动点击裁剪按钮,而是自动进入裁剪模式 且 设定裁剪框为我需要的长方形

Info

RPReplay_Final1730104850.mov

我自己基于v4.5.6 写了一个简单的实现如下。

Configuration code of ZLClipImageViewController

class ZLClipImageViewController: UIViewController {
......
 
 public var editRectFirstLoad: CGRect?
 private var isFistLoad: Bool = true
......

private func layoutInitialImage(animate: Bool) {

......

     // 如果设置了 editRectFirstLoad,在首次加载控件的时候(这时 frame 是覆盖整个待修改图片的),模拟手动调整clip区域
     if let eRectFirstLoad = self.editRectFirstLoad {
         if self.isFistLoad {
             // 保证只执行一次
             self.isFistLoad = false
             
             if frame.size.width > 0 &&
                 eRectFirstLoad.size.width > 0 &&
                 frame.size.height > 0 &&
                 eRectFirstLoad.size.height > 0 &&
                 self.originalImage.size.width > 0 &&
                 self.originalImage.size.height > 0 {
                 
                 // 模拟手动调整clip区域 最终就是 调用 changeClipBoxFrame, 然后再调用 moveClipContentToCenter
                 // 通过 editRectFirstLoad 和 originalImage 计算归一的 rect 比例
                 // 然后 以frame为基础,做调整(在首次加载控件的时候 frame 是覆盖整个待修改图片的)
                 let wRatio = eRectFirstLoad.size.width / self.originalImage.size.width
                 let xRatio = eRectFirstLoad.origin.x / self.originalImage.size.width
                 
                 let hRatio = eRectFirstLoad.size.height / self.originalImage.size.height
                 let yRatio = eRectFirstLoad.origin.y / self.originalImage.size.height
                 
                 var frameFirstLoad = CGRectZero
                 
                 frameFirstLoad.origin.x =       xRatio * frame.size.width  + frame.origin.x
                 frameFirstLoad.origin.y =       yRatio * frame.size.height + frame.origin.y
                 frameFirstLoad.size.width =     wRatio * frame.size.width
                 frameFirstLoad.size.height =    hRatio * frame.size.height
                 
                 //useEditRectFirstLoad = true
                 
                 changeClipBoxFrame(newFrame: frameFirstLoad, animate: true, updateInset: true, endEditing: false)
                 
                 // 模拟手动调整clip区域,endEditing 方法逻辑
                 self.moveClipContentToCenter()
             }
         }
     }

}

Configuration code of ZLEditImageModel

public class ZLEditImageModel: NSObject {
......
 public var editRectFirstLoad: CGRect?
......
 @objc public class func editRectFirstLoadInstance(_ eRect:CGRect) -> ZLEditImageModel {
     let m = ZLEditImageModel(
         drawPaths:  [],
         mosaicPaths:  [],
         clipStatus:  nil,
         adjustStatus:  nil,
         selectFilter:  nil,
         stickers:  [],
         actions:  []
     )
     m.editRectFirstLoad = eRect;
     return m
 }
......
}

Configuration code of ZLEditImageViewController

   @objc public init(image: UIImage, editModel: ZLEditImageModel? = nil) {
......
       preAdjustStatus = currentAdjustStatus
       
       // 从相册选图后 马上进入裁剪模式 且 设定裁剪尺寸

       self.editRectFirstLoad = editModel?.editRectFirstLoad
......
}


   override open func viewDidLoad() {
......
       
       // 从相册选图后 马上进入裁剪模式 且 设定裁剪尺寸
       
       if let eRectFirstLoad = self.editRectFirstLoad {
           DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
               self.useClip(eRectFirstLoad)
           }
       }
   }

   private func clipBtnClick() {
       self.useClip(nil)
   }
   
   private func useClip(_ eRectFrist : CGRect?) {

       preClipStatus = currentClipStatus
       
       let currentEditImage = buildImage()
       let vc = ZLClipImageViewController(image: currentEditImage, status: currentClipStatus)
       let rect = mainScrollView.convert(containerView.frame, to: view)
       vc.presentAnimateFrame = rect
       
       //  从相册选图后 马上进入裁剪模式 且 设定裁剪尺寸
       if eRectFrist != nil {
           vc.editRectFirstLoad = eRectFrist
       }
......
}

以下是调用OC的代码

       CGRect editRect = CGRectZero;
       // 设置 初始裁剪尺寸 editRect
      
       // 初始裁剪尺寸 设置 到 ZLEditImageModel 上(如上面新加的 ZLEditImageModel 构建方法)
       ZLEditImageModel* eModel = [ZLEditImageModel editRectFirstLoadInstance:editRect];
       
       // 拉起系统相册 选图 裁图
       [ZLEditImageViewController showEditImageVCWithParentVC:self
                                                      animate:YES
                                                        image:img
                                                    editModel:eModel
                                                       cancel:nil
                                                   completion:^(UIImage * _Nonnull imgDone, ZLEditImageModel * _Nullable editModel) {
           // 获取到裁剪好的图片
       }];

@longitachi
Copy link
Owner

ZLPhotoConfiguration.default()
    .editImageConfiguration
    .tools([.clip])
    .clipRatios([.wh1x1])
    .showClipDirectlyIfOnlyHasClipTool(true)

ZLPhotoConfiguration.default()
    .maxSelectCount(1)
    .editAfterSelectThumbnailImage(true)

这样就行了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants