簡體   English   中英

使用iOS中的PEPhotoCropEditor庫,恆定的作物縱橫比

[英]Constant crop aspect ratio using PEPhotoCropEditor library in iOS

我在我的一個iOS項目中使用PEPhotoCropEditor庫。 我可以使用下面的代碼使縱橫比保持恆定(1:1)。

- (void)openEditor {

    PECropViewController *controller = [[PECropViewController alloc] init];
    controller.delegate = self;
    controller.image = self.imageView.image;

    UIImage *image = self.imageView.image;   
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    CGFloat length = MIN(width, height);

    CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
    controller.imageCropRect = CGRectMake((width - length) / 2,
                                          (height - length) / 2,
                                          length,
                                          length);


//     Restricted to a square aspect ratio
    controller.keepingCropAspectRatio = YES;
    controller.cropAspectRatio = 1.0;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
    [self presentViewController:navigationController animated:YES completion:NULL];
}

但是然后我需要使寬高比(height:width)為(1:3),而不是(1:1)。

controller.cropAspectRatio = 1.0f / 3.0f;

我在上面嘗試了,沒有任何反應。 仍然具有相同的寬高比。 然后我也更改下面的代碼。

controller.imageCropRect = CGRectMake((width - length) / 2,
                                      (height - length) / 2,
                                      length * 3,
                                      length);

再次沒有任何反應。 在上面評論后,我將值硬編碼如下。

controller.imageCropRect = CGRectMake(0.0f, 0.0f, 300.0f, 100.0f);

然后,裁切區域正確顯示,但在調整aria大小時會導致裁切縱橫比(1:3)

知道如何在所有編輯過程完成之前保持縱橫比(1:3)嗎?

在PECropViewController.m的ViewdidAppear中進行設置

 self.cropAspectRatio = 1.0f;

 self.keepingCropAspectRatio = YES;

並將值傳遞給PECropRectView.m中的函數setKeepingAspectRatio

- (void)setKeepingAspectRatio:(BOOL)keepingAspectRatio
{
   _keepingAspectRatio = keepingAspectRatio;

  if (self.keepingAspectRatio) {
     CGFloat width = CGRectGetWidth(self.bounds);
     CGFloat height = CGRectGetHeight(self.bounds);
     self.fixedAspectRatio = your_fixed_aspect_ratio_value;//fminf(width / height, height / width);
   }
}

您將獲得預期的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM