ios - 裁剪带有羽化边缘的图像
Posted
技术标签:
【中文标题】ios - 裁剪带有羽化边缘的图像【英文标题】:ios - Cropping image with feathered edges 【发布时间】:2014-04-18 17:11:52 【问题描述】:我想裁剪带有羽化圆的图像。我用它来裁剪图像,但它只是裁剪成正方形。
CGImageRef imref = CGImageCreateWithImageInRect([newImage CGImage], faceRect);
newSubImage = [UIImage imageWithCGImage:imref];
我想要的是用羽毛边缘裁剪?我应该用什么来实现它?
【问题讨论】:
看看这是否可以帮助***.com/questions/17161088/… 嘿,这是什么作物? SomeGuy 的回答看起来不错!他明白了,否则赏金会发生***! 【参考方案1】:这个 sn-p 将创建一个带有羽化边缘的圆形切口
第一个 featherLocations
变量可能是您唯一需要调整的变量
- (UIImage *) featheredImageWithImage:(UIImage *) image
// Locations of where the feather starts and ends (0 -> 1)
const CGFloat featherLocations[] = 0.9, 1;
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Draw the original image
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// A 'knock-out' gradient is used to generate a feather effect,
// the alpha channel on the colors defines the alpha of the drawn image
NSArray *gradientColors = @[(id)[UIColor colorWithWhite:0 alpha:1].CGColor,
(id)[UIColor colorWithWhite:0 alpha:0].CGColor];
CGGradientRef gradient = CGGradientCreateWithColors(CGImageGetColorSpace(image.CGImage), (__bridge CFArrayRef)gradientColors, featherLocations);
// Because we're changing the draw mode below,
// take a snapshot of the current draw settings so we can reset them after
CGContextSaveGState(ctx);
// The kCGBlendModeDestinationIn blend mode will provide a'knock-out' effect on
// the previously drawn content, using the alpha channels of the gradient's colors
CGContextSetBlendMode(ctx, kCGBlendModeDestinationIn);
const CGPoint gradientCenter = CGPointMake(image.size.width / 2, image.size.height / 2);
// The gradient will start at the center (0) and extend to the closest edge (horizontal or vertical)
const CGFloat startRadius = 0;
const CGFloat endRadius = MIN(image.size.width,
image.size.height) / 2;
// Draw the gradient to eliminate the pixels we don't want
CGContextDrawRadialGradient(ctx, gradient, gradientCenter, startRadius, gradientCenter, endRadius, (kCGGradientDrawsAfterEndLocation));
CGGradientRelease(gradient);
gradient = NULL;
// Finally, restore state
// (note that in this example CGContextSaveGState and CGContextRestoreGState
// are optional because no further drawing happens after this point)
CGContextRestoreGState(ctx);
// Get the UIImage version
UIImage *featheredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return featheredImage;
【讨论】:
我们有没有可能在不使用阴影和任何颜色渐变的情况下做到这一点?我希望我的图像的边缘(由自定义 uibezierpath 定义)从图像淡入淡出,我不希望它的边界上有阴影或渐变,这可能吗?非常感谢。【参考方案2】:这个呢……第一轮图片:
//round the image
UIImageView *roundView = [[UIImageView alloc] initWithImage:smallImage];
UIGraphicsBeginImageContextWithOptions(roundView.bounds.size, NO, [UIScreen mainScreen].scale);
[[UIBezierPath bezierPathWithRoundedRect:roundView.bounds
cornerRadius:roundView.frame.size.width/2] addClip];
[smallImage drawInRect:roundView.bounds];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然后再次将其四舍五入,但使用较小的圆圈并将贝塞尔路径颜色再次设置为白色,但通过调整 alpha 使其半透明...
【讨论】:
以上是关于ios - 裁剪带有羽化边缘的图像的主要内容,如果未能解决你的问题,请参考以下文章
GlobalMapper精品教程033:影像地图羽化方式详解