iPhone: Clip user-supplied UIImage by a given CGPath

廉价感情. 提交于 2019-12-10 18:13:06

问题


In my iPhone application, I need to let user to clip user-supplied UIImage by given dynamically generated CGPath.

All that is outside given (closed) CGPath should be clipped out, and the resulting image should be trimmed by path's bounding rectangle.

Image should be clipped with soft border. That is, there should be a soft gradient in the alpha channel on the edge of clipping path, from transparent to opaque pixels.

Is there any available solution to achieve described effect?

The main problem is how to get that soft gradient. I thought of rasterizing my CGPath to the mask and blurring it after it was rasterized, but I did not found anything regarding blurring in the iPhone API…

As to clipping, I've planned to use CGPathGetBoundingBox data, but re-reading documentation tells me that the resulting bounding box would include control points for Bézier and quadratic curves, which is not acceptable for me. Well, good old scanning of image for non-zero alpha pixels should do the job instead.


回答1:


For the bounding box, use CGPathGetPathBoundingBox, which does not include the control points.

You will find the filtering (like blurs) in CoreImage.

Basic CoreImage filter usage (from https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_tasks/ci_tasks.html#//apple_ref/doc/uid/TP30001185-CH3-TPXREF101)

CIContext *context = [CIContext contextWithOptions:nil];
CIImage *image = [CIImage imageWithContentsOfURL:myURL];
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"];
[filter setValue:image forKey:kCIInputImageKey];
[filter setValue:@0.8f forKey:kCIInputIntensityKey];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];

Filters:

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/uid/TP40004346



来源:https://stackoverflow.com/questions/1355144/iphone-clip-user-supplied-uiimage-by-a-given-cgpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!