您的位置:首页 > 其它

图片透明度设定

2015-11-20 19:09 330 查看
UIImage *image = [UIImage imageNamed:@"whiteMask"];
image = [image maskImage:[[UIColor blackColor] colorWithAlphaComponent:0.71]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

#import <UIKit/UIKit.h>

@interface UIImage (Mask)

- (UIImage *)maskImage:(UIColor *)maskColor;

@end

#import "UIImage+Mask.h"

@implementation UIImage (Mask)

- (UIImage *)maskImage:(UIColor *)maskColor
{
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextClipToMask(context, rect, self.CGImage);
CGContextSetFillColorWithColor(context, maskColor.CGColor);
CGContextFillRect(context, rect);

UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return smallImage;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: