AarioAi
AarioAi

Reputation: 635

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

When I use CGImageCreateWithImageInRect() to create an CGImage it creates a rectangle 1 px taller and 1 px wider than the rectangle I passed in. Why is this happening?

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

Upvotes: 1

Views: 71

Answers (1)

Dion Larson
Dion Larson

Reputation: 868

Your CGRect has a non-whole number origin. CGImageCreateWithImageInRect will use any pixel that the CGRect overlaps (including the partial pixels).

Upvotes: 2

Related Questions