How to fade corners / edges / borders of a UIImageView

I was not able to find this on the forums, so I decided to post it.

I have a UIImageView with the following code that distorts images from each other.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];

imageNames = [NSArray arrayWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", nil];
imageview.image = [UIImage imageNamed:[imageNames objectAtIndex:0]];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(transitionPhotos) userInfo:nil repeats:YES];

// Rounds corners of imageview
CALayer *layer = [imageview layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:5];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)transitionPhotos{

if (photoCount < [imageNames count] - 1){
    photoCount ++;
}else{
    photoCount = 0;
}
[UIView transitionWithView:imageview
                  duration:2.0
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{imageview.image = [UIImage imageNamed:[imageNames objectAtIndex:photoCount]]; }
                completion:NULL];
}

I want to implement code that will fade the borders of my image.

I know that I can use QuartzCore.framework, but I could not find a guide on how to achieve the result I want.

What will be the shortest code for such a method?

+4
source share
3 answers

You have an image containing a mask (blurred rectangle on a transparent background):

UIImage *mask = [UIImage imageNamed:@"mask.png"];

create maskLayer:

CALayer *maskLayer = [CALayer layer];
maskLayer.contents = (id)mask.CGImage;
maskLayer.frame = imageView.bounds;

and assign it as a mask for your image:

imageView.layer.mask = maskLayer;

: CALayer. shadowRadius shadowPath:

CALayer *maskLayer = [CALayer layer];
maskLayer.frame = imageView.bounds;
maskLayer.shadowRadius = 5;
maskLayer.shadowPath = CGPathCreateWithRoundedRect(CGRectInset(imageView.bounds, 5, 5), 10, 10, nil);
maskLayer.shadowOpacity = 1;
maskLayer.shadowOffset = CGSizeZero;
maskLayer.shadowColor = [UIColor whiteColor].CGColor;

imageView.layer.mask = maskLayer; 
+16

: -

 imageView.layer.cornerRadius = 5;
 imageView.layer.clipsToBounds = YES;
0

You can also try using the GPUImage framework, as stated here.

Here is the code:

UIImage sourceImage = ...
GPUImagePicture *imageSource = [[GPUImagePicture alloc] initWithImage:sourceImage];
GPUImageTransformFilter *transformFilter = [GPUImageTransformFilter new];
GPUImageFastBlurFilter *blurFilter = [GPUImageFastBlurFilter new];

//Force processing at scale factor 1.4 and affine scale with scale factor 1 / 1.4 = 0.7
[transformFilter forceProcessingAtSize:CGSizeMake(SOURCE_WIDTH * 1.4, SOURCE_WIDTH * 1.4)];
[transformFilter setAffineTransform:CGAffineTransformMakeScale(0.7, 0.7)];

//Setup desired blur filter        
[blurFilter setBlurSize:3.0f];
[blurFilter setBlurPasses:20];

//Chain Image->Transform->Blur->Output        
[imageSource addTarget:transformFilter];
[transformFilter addTarget:blurFilter];
[imageSource processImage];

UIImage *blurredImage = [blurFilter imageFromCurrentlyProcessedOutputWithOrientation:UIImageOrientationUp];
0
source

Source: https://habr.com/ru/post/1548245/


All Articles