以编程方式更改UIImage的Alpha

时间:2014-06-26 16:12:49

标签: ios objective-c xcode uiimage splash-screen

我还在探索xCode / Objective-C,我想知道如何才能改变UIImage的alpha值。我尝试编写自己的代码但是我收到错误' Property' alpha'在UIImage'类型的对象上找不到。如果我注释掉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

//谢谢!

2 个答案:

答案 0 :(得分:6)

您需要更改其中包含的UIImageView的字母,UIImage没有alpha属性。

答案 1 :(得分:4)

最佳方式由@JMarsh

显示

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

imageView.image=[self imageByApplyingAlpha: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;
}