button with backgroundimage programmaticaly

UIImage *redButtonImage = [UIImage imageNamed:@"ExitButton.png"];

        UIButton *redButton = [UIButton buttonWithType:UIButtonTypeCustom];
        redButton.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
        [redButton setBackgroundImage:redButtonImage forState:UIControlStateNormal];

        [view addSubview:redButton];

设置图片透明度:

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

 

posted on 2013-08-27 00:21  fishyk  阅读(136)  评论(0编辑  收藏  举报

导航