简体   繁体   中英

How to make an image translucent in iOS?

I have an image in iOS.The image is not too big.Now when i am showing that image as profile pic now i also want to show that profile image as background of current screen with translucent effect.Here translucent is i have seen in iPhone & iPad[![enter image description here][1]][1]. I have tried below code

imageView.alpha=0.3f;

but this does not make the image translucent.

Please tell how can i do this?

To make image translucent you can use visual effect like this

UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

visualEffectView.frame = self.YOURIMAGEVIEW.bounds;
[self.YOURIMAGEVIEW addSubview:visualEffectView];
imageView.backgroundColor = [UIColor clearColor];
imageView.opaque = NO;
imageView.alpha=0.3f;

What you are looking for is UIVibrancyEffect and UIVisualEffectView

What does Alpha do is that it gives you transparency levels being 0 low to 1 high.

You can achieve the required effect through this:

// Create the vibrancy effect - to be added to the blur
let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)
let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)

//Add your contentview as subview
vibrancyEffectView.contentView.addSubview(contentView)
blurEffectView.contentView.addSubview(vibrancyEffectView)

//Then add the blurEffectView to your view hierarchy to display it:
containerView.addSubview(blurEffectView)

Checkout for details: UIVisualEffects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM