swift 扩展用于更改UIImage。这对于UIButton的设置突出显示图像很有用。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 扩展用于更改UIImage。这对于UIButton的设置突出显示图像很有用。相关的知识,希望对你有一定的参考价值。
//
// UIImage+Extension.swift
//
// Created by Yukito Shibuya on 2016/10/08.
// Copyright © 2016年 yukitoto. All rights reserved.
//
import UIKit
extension UIImage {
func imageWithAlphaComponent(alpha: CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, 0)
let ctx = UIGraphicsGetCurrentContext()
let area = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
CGContextScaleCTM(ctx, 1, -1)
CGContextTranslateCTM(ctx, 0, -area.size.height)
CGContextSetBlendMode(ctx, CGBlendMode.Multiply)
CGContextSetAlpha(ctx, alpha)
CGContextDrawImage(ctx, area, self.CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
以上是关于swift 扩展用于更改UIImage。这对于UIButton的设置突出显示图像很有用。的主要内容,如果未能解决你的问题,请参考以下文章