Jonathan Clark
Jonathan Clark

Reputation: 20538

How to convert normal CGRectMake into unit coordinates?

I am developing an iOS app using Rubymotion. I am using the excellent pod/library called GPUImage and I need to crop out a square image with the dimensions of 320 x 320 (squared). I know I can use it like this:

source = GPUImagePicture.alloc.initWithImage(original)

filter = GPUImageCropFilter.alloc.init
filter.setCropRegion(CGRectMake(0.0, 0.0, 1, 1))

source.addTarget(filter)
source.processImage

output = filter.imageFromCurrentlyProcessedOutput

The problem is that I do not know how to convert my ordinary frame coordinates into unit coordinate system that the setCropRegion need to use (0.0 - 1.0). I know there should be some sort of algoritm.

The frame coordinates I am trying to convert are:

CGRectMake(10, 10, 320, 320)

Upvotes: 0

Views: 352

Answers (1)

Ben Trengrove
Ben Trengrove

Reputation: 8729

Just think of them as percentages.

It will be:

CGRectMake(10.0/imageWidth, 10.0/imageHeight, 320.0/imageWidth, 320.0/imageHeight);

Upvotes: 1

Related Questions