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

时间:2014-07-13 11:04:36

标签: ios objective-c uiimageview

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

我有一个带有以下代码的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];
}

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

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

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

3 个答案:

答案 0 :(得分:16)

让图像包含遮罩(透明背景上的矩形模糊):

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

创建一个maskLayer:

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

并将其指定为imageView的掩码:

imageView.layer.mask = maskLayer;

编辑: 您可以使用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; 

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您也可以尝试使用GPUImage框架,如on this answer here.

所述

以下是代码:

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