簡體   English   中英

如何設置UIImage的Alpha?

[英]How can I set the Alpha of a UIImage?

當我合並兩個UIImage時(使用drawinrect:),它們都是相同的alpha值,即使一個值小於1.0。 如何更改專門用於UIImage的Alpha?

您不能更改UIImage的Alpha。 您可以使用alpha將其繪制到新的上下文並從中獲得新的圖像。 或者,您可以提取CGImage,然后提取數據,然后調整alpha字節,然后根據數據創建新的CGImage,並根據CGImage創建新的UIImage。

但是在這種情況下,只需使用drawInRect:blendMode:alpha:而不是drawInRect:

這是UIImage類別。

用法>>

UIImage * imgNew = [imgOld cloneWithAlpha:.3];

代碼>>

- (UIImage *)cloneWithAlpha:(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;
    }

ref >> 如何設置UIImage的不透明度/ alpha?

如果UIImage顯示在UIImageView中,則可以在UIImageView上設置alpha屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM