3

我想在裁剪时对图像应用羽毛效果,但希望我使用 UIGraphicsImageRenderer 渲染图像,渲染为 nil(iOS 9.3)是否还有其他选项可以在 iOS 9.3 中渲染图像

这是我的代码:

UIImage* Img = _myimage;

UIImageView * imageview = [[UIImageView alloc]initWithImage:Img];

[imageview setFrame:CGRectMake(0, 0, Img.size.width, Img.size.height)];

UIBezierPath *aPath;

CAShapeLayer *shapeLayer;
aPath = [UIBezierPath bezierPath];
aPath.contractionFactor = 0.8;
NSValue *mvalue = [_pointsarray objectAtIndex:0];
CGPoint mp2 = [mvalue CGPointValue];
[aPath moveToPoint:mp2];
[aPath addBezierThroughPoints:_pointsarray];
[aPath closePath];

aPath.lineWidth = 2;

shapeLayer = [CAShapeLayer new];

shapeLayer.path=aPath.CGPath;

[shapeLayer setFillColor:[UIColor redColor].CGColor];

[shapeLayer fillColor];

//这里的渲染器在 iOS 9.3 中是 nil

UIGraphicsImageRenderer * renderer = [[UIGraphicsImageRenderer alloc] initWithSize:Img.size];

UIImage *shapeImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context)
{

    [shapeLayer renderInContext: context.CGContext];

}];


CIImage * shapeCimage = [[CIImage alloc] initWithImage:shapeImage];
CIFilter * gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:shapeCimage forKey: @"inputImage"];
[gaussianBlurFilter setValue:@8 forKey:@"inputRadius"];

CIImage * blurredCIImage = [gaussianBlurFilter valueForKey:kCIOutputImageKey];
UIImage * blurredImage = [UIImage imageWithCIImage:blurredCIImage];

UIImageView *maskedImageView = [[UIImageView alloc]initWithImage:blurredImage];
maskedImageView.contentMode = UIViewContentModeScaleAspectFit;
maskedImageView.frame = imageview.frame;
imageview.layer.mask=maskedImageView.layer;

[self.view addSubview:imageview];
4

1 回答 1

4

UIGraphicsImageRenderer在 iOS 9 中不可用。如果您需要针对此,您可以轻松编写自己的代码:

+(instancetype)LT_imageByDrawingOnCanvasOfSize:(CGSize)size usingBlock:(LTImageDrawingContextBlock)block
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    block( context );

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
于 2017-12-25T15:37:04.983 回答