2011-02-22 130 views

回答

114

我只是需要这样做,但认为史蒂文的解决方案会很慢。这应该有希望使用图形硬件。在UIImage的创建类别:

- (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; 
} 
+2

甚至不记得为什么我需要这个:) – Marty 2012-05-31 02:48:15

+0

伟大的解决方案!谢谢尼克! – Christopher 2012-06-18 22:00:57

+1

只需确保您在主线程上调用UIGraphicsBeginImageContextWithOptions,因为背景渲染将是不可预知的。 – 2012-11-15 09:31:50

72

设置其视图的不透明度则表现出。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"SomeName.png"]]; 
imageView.alpha = 0.5; //Alpha runs from 0.0 to 1.0 

使用该动画中。您可以在一段时间内更改动画中的Alpha。

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:1.0]; 
//Set alpha 
[UIView commitAnimations]; 
+0

是啊,我想我可能有一个计时器不断地改变不透明度为像这样做,我想这一定是为动画图像数组属性设置一个数组更容易,因此它可以自己动画。 – Marty 2011-02-22 22:49:57

+0

您可以使用动画代理将动画与'心跳'一起动画。不要使用定时器,动画会顺利地改变alpha。祝你好运。 – 2011-02-22 22:52:27

+3

这只适用于开发人员使用UIImageView的情况。这个问题清楚地表明情况并非如此。 – 2014-08-26 20:29:25

4

我意识到这是相当晚了,但我需要这样的东西,所以我掀起了一个快速和肮脏的方法来做到这一点。

+ (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; 
} 
+2

更好的名字将会是'setImage:withAlpha:' – Alexander 2012-05-25 09:59:26

+0

如此真实。感谢提示 – 2012-05-28 04:28:19

+8

集通常指的是属性,以某种方式改变接收者的状态。最好命名为'image:withAlpha:'? – 2012-09-02 19:20:18

10

有很多简单的解决方案:

- (UIImage *)tranlucentWithAlpha:(CGFloat)alpha 
{ 
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); 
    [self drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:alpha]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 
+1

可能应该是被接受的答案。最快/最有效的方法来获得结果 – 2017-12-14 18:47:31

26

基于阿列克谢Ishkov的回答,但是在斯威夫特

我用的UIImage类的扩展。

斯威夫特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) 

斯威夫特3:

注意,此实现收益一个可选的UIImage。这是因为在Swift 3中UIGraphicsGetImageFromCurrentImageContext现在返回一个可选项。如果上下文为零,或者不使用UIGraphicsBeginImageContext创建,则此值可能为零。

的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 
    } 
} 

要使用:从Xamarin用户

let image = UIImage(named: "my_image") 
let transparentImage = image?.image(alpha: 0.5) 
+0

我更喜欢这个'Swift 3'版本。 – AechoLiu 2017-11-03 08:28:28

3

嘿,嘿,谢谢! :)这里有云转换为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);