iOS 9.3中的UIGraphicsImageRenderer显示为空,还有其他选项吗?

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

请阅读以下内容:https://developer.apple.com/documentation/uikit/uigraphicsimagerenderer。它说需要iOS10或更高版本。 - user5890979
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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接