0 Notes & Comments

Cocoa Snippet: Image resize with CI

Core Image provides a lot of filter to apply to images. Here there is a snippet to modify the size of the image using the Lanczos algorithm. You have to give the image URL and the ratio of the scale. If the ratio is smaller than 1 it reduce, otherwise it enlarges the image.

// Resize the image
CIImage *ciImage = [CIImage imageWithContentsOfURL:<#ImageURL#>];
CIFilter *scaleFilter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
[scaleFilter setValue:ciImage forKey:@"inputImage"];
[scaleFilter setValue:[NSNumber numberWithFloat:<#ratio#>] forKey:@"inputScale"];
[scaleFilter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputAspectRatio"];
CIImage *finalImage = [scaleFilter valueForKey:@"outputImage"];

More information may be found at the Core Image Filter Reference.