如何设置UIImage的不透明度/ alpha值?
我知道你可以用UIImageView做到这一点,但可以做到UIImage? 我想要一个UIImageView的animation图像数组属性是一个相同的图像,但不同的不透明度的数组。 思考?
我只是需要这样做,但认为史蒂文的解决scheme会很慢。 这应该有希望使用graphics硬件。 在UIImage上创build一个类别:
- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha { UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect area = CGRectMake(0, 0, self.size.width, self.size.height); CGContextScaleCTM(ctx, 1, -1); CGContextTranslateCTM(ctx, 0, -area.size.height); CGContextSetBlendMode(ctx, kCGBlendModeMultiply); CGContextSetAlpha(ctx, alpha); CGContextDrawImage(ctx, area, self.CGImage); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
设置其显示的视图的不透明度。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"SomeName.png"]]; imageView.alpha = 0.5; //Alpha runs from 0.0 to 1.0
在animation中使用这个。 您可以在一段时间内更改animation中的Alpha。
[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1.0]; //Set alpha [UIView commitAnimations];
根据阿列克谢·伊什科夫的回答,但在斯威夫特
我使用了UIImage类的扩展。
Swift 2:
UIImage扩展:
extension UIImage { func imageWithAlpha(alpha: CGFloat) -> UIImage { UIGraphicsBeginImageContextWithOptions(size, false, scale) drawAtPoint(CGPointZero, blendMode: .Normal, alpha: alpha) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } }
使用:
let image = UIImage(named: "my_image") let transparentImage = image.imageWithAlpha(0.5)
Swift 3:
请注意,这个实现返回一个可选的UIImage。 这是因为在Swift 3 UIGraphicsGetImageFromCurrentImageContext
现在返回一个可选项。 如果上下文为零,或者不是用UIGraphicsBeginImageContext
创build的,则此值可以为零。
UIImage扩展:
extension UIImage { func image(alpha: CGFloat) -> UIImage? { UIGraphicsBeginImageContextWithOptions(size, false, scale) draw(at: .zero, blendMode: .normal, alpha: alpha) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } }
使用:
let image = UIImage(named: "my_image") let transparentImage = image?.image(alpha: 0.5)
有更简单的解决scheme:
- (UIImage *)tranlucentWithAlpha:(CGFloat)alpha { UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); [self drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:alpha]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
我意识到这是相当晚的,但我需要这样的东西,所以我掀起了一个快速和肮脏的方法来做到这一点。
+ (UIImage *) image:(UIImage *)image withAlpha:(CGFloat)alpha{ // Create a pixel buffer in an easy to use format CGImageRef imageRef = [image CGImage]; NSUInteger width = CGImageGetWidth(imageRef); NSUInteger height = CGImageGetHeight(imageRef); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); UInt8 * m_PixelBuf = malloc(sizeof(UInt8) * height * width * 4); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(m_PixelBuf, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); CGContextRelease(context); //alter the alpha int length = height * width * 4; for (int i=0; i<length; i+=4) { m_PixelBuf[i+3] = 255*alpha; } //create a new image CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGImageRef newImgRef = CGBitmapContextCreateImage(ctx); CGColorSpaceRelease(colorSpace); CGContextRelease(ctx); free(m_PixelBuf); UIImage *finalImage = [UIImage imageWithCGImage:newImgRef]; CGImageRelease(newImgRef); return finalImage; }
嘿谢谢Xamarin用户! :)在这里它被转换为C#
//*************************************************************************** public static class ImageExtensions //*************************************************************************** { //------------------------------------------------------------- public static UIImage WithAlpha(this UIImage image, float alpha) //------------------------------------------------------------- { UIGraphics.BeginImageContextWithOptions(image.Size,false,image.CurrentScale); image.Draw(CGPoint.Empty, CGBlendMode.Normal, alpha); var newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return newImage; } }
用法示例:
var MySupaImage = UIImage.FromBundle("opaquestuff.png").WithAlpha(0.15f);