3

I'm want apply feather effect to the image when cropped but want i render the image using UIGraphicsImageRenderer the render is nil(iOS 9.3) is there any other option for rendering the image in iOS 9.3

This is my code:

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

//Here the Renderer is nil in 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];
Mitesh Varu
  • 123
  • 1
  • 10
  • https://developer.apple.com/documentation/uikit/uigraphicsimagerenderer Read. it says iOS10+ –  Jul 29 '17 at 15:19

1 Answers1

4

UIGraphicsImageRenderer is not available in iOS 9. If you need to target this, you can easily write your own like that:

+(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;
}
DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67