iOS 9.3 UIGraphicsImageRenderer为此显示nil任何其他选项?

时间:2017-05-20 05:33:59

标签: objective-c effect renderer gaussianblur feather

我想在裁剪时对图像应用羽毛效果,但是想要使用UIGraphicsImageRenderer渲染图像,渲染为零(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中为零

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];

1 个答案:

答案 0 :(得分:2)

iOS 9中不提供

UIGraphicsImageRenderer。如果您需要定位,可以轻松编写自己的内容:

+(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;
}