【问题标题】:Image Cropping Issue for Cropping an Image裁剪图像的图像裁剪问题
【发布时间】:2017-05-22 00:23:18
【问题描述】:

我想裁剪一张图片,我搜索了很多库但没有找到完美的答案。

我想使用矩形裁剪图像,该矩形将具有固定的最大高度和宽度(300、200)以及固定的最小高度和宽度(1.0、1.0)。该矩形也是可移动的,并且可以在最大和最小尺寸范围内调整为任何尺寸。

【问题讨论】:

  • 您是想为这项任务寻找一个应用程序,还是想创建一个应用程序来执行此任务?如果您正在尝试创建应用程序,您使用的是什么语言,您已经尝试过什么?
  • 我用过Objective-c..
  • 我不太确定您已经编写了多少代码,但这有帮助吗? stackoverflow.com/questions/18205672/…
  • 其实我没有写任何代码,但我想在objective-c中这样做。我没有完美的图书馆...

标签: ios objective-c iphone


【解决方案1】:

您可以使用 TimOliver/TOCropViewController 并根据 TOCropViewController.m 类的需求更改您的裁剪框,

1:从https://github.com/TimOliver/TOCropViewController下载完整项目。

2:拖拽TOCropViewController.h、TOCropViewController.m和类与其他类。

3:在TOCropViewController.m类中根据需求设置比例,方法为[self setAspectRatioPreset:self.aspectRatioPreset animated:NO];

【讨论】:

  • 这是一个漫长的过程,但非常适合裁剪。
  • 您能否提供编码部分,矩形的最大高度和宽度(300、200)和固定的最小高度和宽度(1.0、1.0)并且可以移动。
  • 你可以设置比例 3:2 on - (void)setAspectRatioPreset:(TOCropViewControllerAspectRatioPreset)aspectRatioPreset animated:(BOOL)animated switch (aspectRatioPreset) { case TOCropViewControllerAspectRatioPresetOriginal: // aspectRatio = CGSizeZero; aspectRatio = CGSizeMake(3.0f, 2.0f);} 并且可以按这个比例移动。
【解决方案2】:

我只能部分理解您的问题,如果您想裁剪 UIImage,请查看以下代码:

//Pass the UIImage object and the varying rectangle you "outputrect"
     - (UIImage *)cropImage:(UIImage *)image outPutRect:(CGRect) outputRect
    {

         CGImageRef takenCGImage = image.CGImage;
        size_t width = CGImageGetWidth(takenCGImage);
        size_t height = CGImageGetHeight(takenCGImage);
        CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height,
                                     outputRect.size.width * width, outputRect.size.height * height);

        CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect);
        image = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:image.imageOrientation];
        CGImageRelease(cropCGImage);

        return image;
    }

【讨论】:

  • 我想动态裁剪矩形形状的图像,矩形有固定的最大高度和宽度(300、200)和固定的最小高度和宽度(1.0、1.0)。矩形也将是可移动的,并且可以调整为最大和最小尺寸内的任何尺寸。
  • 您可以在上述方法中将您的可移动矩形作为“输出矩形”传递,它会将裁剪为该输出矩形的 UIImage 返回给您。请投票。干杯。
猜你喜欢
  • 2013-03-05
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-04-19
  • 2022-01-23
  • 2023-04-02
  • 2010-10-24
相关资源
最近更新 更多