xcode 以编程方式更改 UIImage 的 Alpha

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24435477/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:07:55  来源:igfitidea点击:

Change Alpha of a UIImage Programmatically

iosobjective-cxcodeuiimagesplash-screen

提问by BruceTheGoose

I'm still exploring xCode/Objective-C, And I was wondering how would I be able to change the alpha of a UIImage. I tried writing my own code but I get the error 'Property 'alpha' not found on object of type UIImage'. The splash screen works if I comment out the alpha changing lines. My code is below.

我仍在探索 xCode/Objective-C,我想知道如何更改 UIImage 的 alpha。我尝试编写自己的代码,但出现错误“在 UIImage 类型的对象上找不到属性 'alpha'”。如果我注释掉 alpha 更改行,启动屏幕会起作用。我的代码如下。

//
//  ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,weak) IBOutlet UIImage *logo;

@end

@implementation ViewController

ADBannerView *_bannerView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:_bannerView];

    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(showLogo:) userInfo:nil repeats:NO];

}

- (void)showLogo:(NSTimer *) timer{
    [UIView animateWithDuration:2.0 animations:^{
    self.logo.alpha = 1.0;
} completion:^(BOOL finished){

    [UIView animateWithDuration:2.0 animations:^{
        self.logo.alpha = 0.0;
    } completion:^(BOOL finished){
        ViewController *controller  = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
        [self.navigationController pushViewController:controller animated:YES];
    }];

}];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//Thank You!

//谢谢你!

回答by JMarsh

You need to change the alpha of the UIImageViewit is contained in, there is no alpha property for UIImage.

您需要更改UIImageView它所包含的 alpha,没有 alpha 属性UIImage

回答by user3772344

Best way is shown by @JMarsh

@JMarsh 展示了最佳方式

I have found method that change the alpha value. you can try this.

我找到了改变 alpha 值的方法。你可以试试这个。

imageView.image=[self imageByApplyingAlpha:image]

//Change alpha of image

//改变图像的alpha

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