Swift 头像
Swift  2016-03-27 22:34

Swift 处理图片的类库 AFImageHelper

回复:0  查看:5091  感兴趣:34  赞:2  

Swift 处理图片的类库,Swift 实现 UIImage 和 UIImageView 的扩展。可以对图片的压缩、颜色、渐变、裁剪等操作进行了扩展,以及具有缓存机制的在线图片获取。

Swift 处理图片的类库 AFImageHelper

UIImageView 扩展


// Fetches an image from a URL. If caching is set, it will be cached by NSCache for future queries. The cached image is returned if available, otherise the placeholder is set. When the image is returned, the closure gets called.
func imageFromURL(url: String, placeholder: UIImage, fadeIn: Bool = true, closure: ((image: UIImage?)
UIImage 扩展


// Creates an image from a solid color
UIImage(color:UIColor, size:CGSize)

// Creates an image from a gradient color
UIImage(gradientColors:[UIColor], size:CGSize) 

// Applies a gradient overlay to an image
func applyGradientColors(gradientColors: [UIColor], blendMode: CGBlendMode) -> UIImage 

// Creates an image from a radial gradient
UIImage(startColor: UIColor, endColor: UIColor, radialGradientCenter: CGPoint, radius:Float, size:CGSize)
// Creates an image with a string of text
UIImage(text: String, font: UIFont, color: UIColor, backgroundColor: UIColor, size:CGSize, offset: CGPoint)
// Creates an image from a UIView 
UIImage(fromView view: UIView)
// Returns true if the image has an alpha layer
func hasAlpha() -> Bool

// Returns a copy(if needed) of the image with alpha layer 
func applyAlpha() -> UIImage? 

// Returns a copy of the image with a transparent border of the given size added around its edges
func applyPadding(padding: CGFloat) -> UIImage?
// Crops an image to a new rect
func crop(bounds: CGRect) -> UIImage?

// Crops an image to a centered square
func cropToSquare() -> UIImage? {

// Resizes an image
func resize(size:CGSize, contentMode: UIImageContentMode = .ScaleToFill) -> UIImage? 
// Rounds corners of an image
func roundCorners(cornerRadius:CGFloat) -> UIImage?

// Rounds corners of an image with border
func roundCorners(cornerRadius:CGFloat, border:CGFloat, color:UIColor) -> UIImage?

// Rounds corners to a circle
func roundCornersToCircle() -> UIImage?

// Rounds corners to a circle with border
func roundCornersToCircle(border border:CGFloat, color:UIColor) -> UIImage?
// Adds a border
func applyBorder(border:CGFloat, color:UIColor) -> UIImage?
// To create an image that is Retina aware, use the screen scale as a multiplier for your size. You should also use this technique for padding or borders.
let width = 140 * UIScreen.mainScreen().scale
let height = 140 * UIScreen.mainScreen().scale
let image = UIImage(named: "myImage")?.resize(CGSize(width: width, height: height))