imageByCroppingToRect:

返回一个包含原始图像裁剪部分的新图像。

SDK Version
iOS 5.0+
macOS 10.5+
tvOS 9.0+

声明

  1. /* Objective-C */
  2. - (CIImage *)imageByCroppingToRect:(CGRect)rect;
  1. /* Swift */

参数

  • rect

    图像坐标中用于裁剪图像的矩形。


返回值

一个裁剪到指定矩形的图像对象。

裁剪一个图像


说明

由于Core Image的坐标系与UIKit不匹配,当使用contentModeUIImageView中显示时,此过滤方法可能会产生意外结果。请务必使用CGImage对其进行备份,以便正确处理contentMode

  1. /* Objective-C */
  2. CIContext* context = [CIContext context];
  3. CGImageRef cgCroppedImage = [context createCGImage:ciCroppedImage fromRect:ciCroppedImage.extent];
  4. UIImage* croppedImage = [UIImage imageWithCGImage:cgCroppedImage];
  5. CGImageRelease(cgCroppedImage);
  1. /* Swift */

如果你主要以CGImageRefUIImage显示或处理图像,而没有额外的Core Image应用程序,请考虑使用CGImageCreateWithImageInRect函数在Core Graphics中裁剪,以节省从图像转换到CIImage的处理开销。当你的管道中已经有CIImage时最好是使用imageByCroppingToRect: