2011-02-22 131 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);