-
Cropping image without degrading their quality (Using CIFilter's CICrop filter)
over 11 years ago
over 11 years ago
The below mentioned code uses CIImage library's CICrop filter to crop the desired area from UIImage. The follwing code deoesn't deals with snapshot as CGContextRef rather crops the original image along with maintaining its quality and aspect ratio.
- CGImageRef imageRef = [[UIImage imageNamed@"your_image"] CGImage];
- CIImage *image = [CIImage imageWithCGImage:imageRef];
- CIFilter *scaleFilter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
- int originalHeight=[image extent].size.height;
- int originalWidth=[image extent].size.width;
- float yScale=screenHeight / (float)originalHeight;
- float xScale=screenWidth / (float)originalWidth;
- float scale=fminf(yScale, xScale);
- [scaleFilter setValue:[NSNumber numberWithFloat:scale]
- forKey:@"inputScale"];
- [scaleFilter setValue:[NSNumber numberWithFloat:1.0]
- forKey:@"inputAspectRatio"];
- [scaleFilter setValue: image
- forKey:@"inputImage"];
- UIImage *outputImage = [scaleFilter valueForKey:@"outputImage"];
CGImageRef imageRef = [[UIImage imageNamed@"your_image"] CGImage]; CIImage *image = [CIImage imageWithCGImage:imageRef]; CIFilter *scaleFilter = [CIFilter filterWithName:@"CILanczosScaleTransform"]; int originalHeight=[image extent].size.height; int originalWidth=[image extent].size.width; float yScale=screenHeight / (float)originalHeight; float xScale=screenWidth / (float)originalWidth; float scale=fminf(yScale, xScale); [scaleFilter setValue:[NSNumber numberWithFloat:scale] forKey:@"inputScale"]; [scaleFilter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputAspectRatio"]; [scaleFilter setValue: image forKey:@"inputImage"]; UIImage *outputImage = [scaleFilter valueForKey:@"outputImage"];
Can you help out the community by solving one of the following Social Media problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)