如何淡化UIImageView的角/边/边框

use*_*052 4 objective-c uiimageview ios

我无法在论坛上找到这个,所以我决定发布这个.

我有一个UIImageView与以下代码相互淡化图像.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

我想实现一个淡化我的imageview边框的代码.

我知道我可以使用QuartzCore.framework,但是我无法找到如何实现我想要的结果的指南.

什么是最短的代码来做这样的方法?

rob*_*ert 16

有一个包含掩码的图像(透明背景上的模糊矩形):

UIImage *mask = [UIImage imageNamed:@"mask.png"];
Run Code Online (Sandbox Code Playgroud)

创建一个maskLayer:

CALayer *maskLayer = [CALayer layer];
maskLayer.contents = (id)mask.CGImage;
maskLayer.frame = imageView.bounds;
Run Code Online (Sandbox Code Playgroud)

并将其指定为imageView的掩码:

imageView.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)

编辑:您可以使用阴影属性完全通过代码创建蒙版CALayer.只是玩弄shadowRadiusshadowPath:

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; 
Run Code Online (Sandbox Code Playgroud)


Hus*_*bir -1

对于圆角,您需要修改以下代码:-

 imageView.layer.cornerRadius = 5;
 imageView.layer.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)