Swift:为什么 CGImageCreateWithImageInRect() 创建了一个大 1 像素的矩形?

Swift: Why CGImageCreateWithImageInRect() creates a rectangle 1 more pix larger?

当我使用 CGImageCreateWithImageInRect() 创建 CGImage 时,它​​创建的矩形比我传入的矩形高 1 px 宽 1 px。为什么会这样?

let rect: CGRect = CGRectMake(CGFloat(124.583333333333), CGFloat(156.416666666667), CGFloat(180.0), CGFloat(180.0))
let imgRef: CGImageRef = CGImageCreateWithImageInRect(img.CGImage, rect)!
print(rect.size)     // 180,180
print(CGImageGetWith(imgRef), CGImageGetHeight(imgRef))  // 181, 181

您的 CGRect 的来源不是整数。 CGImageCreateWithImageInRect 将使用 CGRect 重叠的任何像素(包括部分像素)。