014-防止图片拉伸或者缩放显示而影响性能

1、应用场景:尤其是在表格中动态显示图片的时候,随着表格的滚动,图片要‘适应’的显示在UIImageView中,这要每次计算就会影响表格性能

1-1、UIImageView+WebImage

//
//  UIImageView+WebImage.swift
//
//  Created by 紫荆秋雪 on 17/2/1.
//  Copyright © 2017年 紫荆秋雪. All rights reserved.
//

import SDWebImage

extension UIImageView {

    /// 设置带有圆圈的用户头像
    ///
    /// - Parameters:
    ///   - urlString: url
    ///   - placeholderImage: 占位图片
    func Revan_setAvatarImage(urlString: String?, placeholderImage: UIImage?) {
        //处理 URL
        guard let urlString = urlString, let url = URL(string: urlString) else {
            //直接设置占位图片
            image = placeholderImage
            return
        }
        
        sd_setImage(with: url, placeholderImage: placeholderImage, options: [], progress: nil) { [weak self] (image, _, _, _) in
            self?.image = image?.Revan_avatarImage(size: self?.bounds.size)
        }
    }
    
    func Revan_setImage(urlString: String?, placeholderImage: UIImage?) {
        //处理 URL
        guard let urlString = urlString, let url = URL(string: urlString) else {
            //直接设置占位图片
            image = placeholderImage
            return
        }
        
        sd_setImage(with: url, placeholderImage: placeholderImage, options: [], progress: nil) { [weak self] (image, _, _, _) in
            self?.image = image?.Revan_image(size: self?.bounds.size)
        }
    }

}

1-2、UIImage+Extensions

//
//  UIImage+Extensions.swift
//
//  Created by 紫荆秋雪 on 17/2/1.
//  Copyright © 2017年 紫荆秋雪. All rights reserved.
//

import Foundation

extension UIImage {
    /// 创建头像图像
    ///
    /// - Parameters:
    ///   - size: 尺寸
    ///   - backColor: 背景颜色
    ///   - lineColor: 头像圆圈颜色
    /// - Returns: 裁切后的图像
    func Revan_avatarImage(size: CGSize?, backColor: UIColor = UIColor.white, lineColor: UIColor = UIColor.lightGray) -> UIImage? {
        var size = size
        if size == nil {
            size = self.size
        }
        let rect = CGRect(origin: CGPoint(), size: size!)
        // 创建“图形上下文”
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        // 设置填充颜色
        backColor.setFill()
        UIRectFill(rect)
        
        // 设置剪切路径
        let path = UIBezierPath(ovalIn: rect)
        // 裁剪
        path.addClip()
        // 画图
        draw(in: rect)
         // 设置剪切路径样式
        let ovalPath = UIBezierPath(ovalIn: rect)
        ovalPath.lineWidth = 2
        lineColor.setStroke()
        ovalPath.stroke()
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
    
    
    /// 创建新图像
    ///
    /// - Parameters:
    ///   - size: 尺寸
    ///   - backColor: 背景颜色
    /// - Returns: 新图像
    func Revan_image(size: CGSize?, backColor: UIColor = UIColor.white) -> UIImage? {
        var size = size
        if size == nil {
            size = self.size
        }
        let rect = CGRect(origin: CGPoint(), size: size!)
        // 创建“图形上下文”
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        // 设置填充颜色
        backColor.setFill()
        UIRectFill(rect)
        
        // 设置剪切路径
//        let path = UIBezierPath(ovalIn: rect)
//        // 裁剪
//        path.addClip()
        // 画图
        draw(in: rect)
        // 设置剪切路径样式
//        let ovalPath = UIBezierPath(ovalIn: rect)
//        ovalPath.lineWidth = 2
//        lineColor.setStroke()
//        ovalPath.stroke()
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
    
}

你可能感兴趣的:(014-防止图片拉伸或者缩放显示而影响性能)