xcode 使 UIImageView IOS 变暗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14429646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Darken UIImageView IOS
提问by Alessandro
what I was trying to do was to make a UIImageView darker so that the buttons and views on top of the image result. I wanted to give it the effect that is given when in ios6 a user shares using Facebook. All the content behind it becomes darker but it is visible. I tried using:
我试图做的是使 UIImageView 变暗,以便图像顶部的按钮和视图产生效果。我想赋予它在 ios6 中用户使用 Facebook 共享时给出的效果。它背后的所有内容都变得更暗,但它是可见的。我尝试使用:
UIImage *maskedImage = [self darkenImage:image toLevel:0.5];
- (UIImage *)darkenImage:(UIImage *)image toLevel:(CGFloat)level
{
// Create a temporary view to act as a darkening layer
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
UIView *tempView = [[UIView alloc] initWithFrame:frame];
tempView.backgroundColor = [UIColor blackColor];
tempView.alpha = level;
// Draw the image into a new graphics context
UIGraphicsBeginImageContext(frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:frame];
// Flip the context vertically so we can draw the dark layer via a mask that
// aligns with the image's alpha pixels (Quartz uses flipped coordinates)
CGContextTranslateCTM(context, 0, frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextClipToMask(context, frame, image.CGImage);
[tempView.layer renderInContext:context];
// Produce a new image from this context
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *toReturn = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
UIGraphicsEndImageContext();
return toReturn;
}
but it doesn't give the effect I want. It changes the colors, while I just want it to become darker, like when you use the ios facebook sharing, and the background becomes dark. Is this possible?
但它没有给出我想要的效果。它改变了颜色,而我只是想让它变暗,就像你使用 ios facebook 共享时一样,背景变暗。这可能吗?
回答by Mecki
To make a UIView, all its contents and subview, darker, you simply draw another UIView over it that is all black and has an alpha transparency, so that the black color of this "shielding view" mixes with the contents of the view below. If the alpha transparency is 0.5, everything below will look like it darkened by 50%.
要使 UIView 及其所有内容和子视图变暗,您只需在其上绘制另一个全黑且具有 alpha 透明度的 UIView,以便此“屏蔽视图”的黑色与下面视图的内容混合。如果 alpha 透明度为 0.5,则下面的所有内容看起来都变暗了 50%。
To get a fading effect, just use a transition of the "shielding view":
要获得褪色效果,只需使用“屏蔽视图”的过渡:
// Make the view 100% transparent before you put it into your view hierarchy.
[myView setAlpha:0];
// Layout the view to shield all the content you want to darken.
// Give it the correct size & position and make sure is "higher" in the view
// hierarchy than your other content.
...code goes here...
// Now fade the view from 100% alpha to 50% alpha:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[myView setAlpha:0.5];
[UIView commitAnimations];
You can change the animation duration to make the fade slower or faster as needed. If you don't set any animation duration, the default duration is being used (which is the case when you Facebook sharing I guess).
您可以根据需要更改动画持续时间以使淡入淡出变慢或变快。如果您未设置任何动画持续时间,则将使用默认持续时间(我猜您在 Facebook 分享时就是这种情况)。