Swift - UI

5、UIImage及资源名管理、UIImageView

2020-03-07  本文已影响0人  爱玩游戏的iOS菜鸟

UIImage

UIImage的初始化

顾名思义,就是图片类,继承自NSObject,不能直接展示到视图上,必须借助图片的视图容器才能展示
UIKit标准库中可以看到提供了很多种方式进行初始化UIImage对象,下面

//可失败初始化器
init?(systemName name: String)//以系统符号图像创建(基本不用)
init?(named name: String)//以Assets资源文件中的资源名创建
init?(contentsOfFile path: String)//从文件目录中的图片资源名
init?(data: Data, scale: CGFloat)//通过数据流初始化图片

代码示例:

//以Assets资源文件中的资源名创建
  let image = kImageName("working_报表")

//从文件目录中的图片资源名
  let path = Bundle.main.path(forResource: "new_feature@2x", ofType: "png")
  var fileImage: UIImage?
  if let confirmPath = path {
    fileImage = UIImage(contentsOfFile: confirmPath)
  }

 //通过数据流初始化图片       
  let url = URL(string: "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2716614305,3713499237&fm=15&gp=0.jpg")
  let data = try? Data(contentsOf: url!)
  var urlImage: UIImage?
  if let confirmData = data {
    urlImage = UIImage(data: confirmData)
  }
UIImage的常用属性

其中RenderingMode枚举值有3个:

  let image = kImageName("working_报表")
  image?.size//输出:Optional((25.0, 24.0))
  image?.scale//输出: Optional(3.0)
  image?.isSymbolImage//输出: Optional(false)
  image.renderingMode = UIImage.RenderingMode.alwaysOriginal

可选链:可选项包含值,属性、方法或者下标的调用成功,结果会被包装为可选项
可选链

CGImage CIImage与UIImage

在标准库中,用于CGImage与CIImage初始化UIImage各一个初始化器

//初始化器  CGImage CIImage 与UIImage的相互转换
init(cgImage: CGImage, scale: CGFloat, orientation: UIImage.Orientation)
init(ciImage: CIImage, scale: CGFloat, orientation: UIImage.Orientation)
  1. CIImage与UIImage

代码调用:

  let image = kImageName("working_报表")
  let ciImage = CIImage(image: image!)
  let ciImageTrans = UIImage(ciImage: ciImage!)

  let ciNewImage = ciImageTrans.ciImage
  kLog(ciNewImage == ciImage)//true

【注意】UIImage对象的CIImage属性可能为空,因为UIImage对象可能由CGImage初始化,此时的ciImage = nil

  1. CGImage与UIImage
  let image = kImageName("working_报表")
  let cgImage = image?.cgImage
  let cgImageTrans = UIImage(cgImage: cgImage!)

【注意】UIImage对象的CGImage属性可能为空,因为UIImage对象可能由CIImage初始化,此时的cgImage = nil

这时候将二者对比一下,转化为Data类型

  let image = kImageName("working_报表")
        
  let ciImage = CIImage(image: image!)
  let ciImageTrans = UIImage(ciImage: ciImage!)
  let cidata = ciImageTrans.pngData()
  kLog(cidata)//Optional(4279 bytes)
  kLog(ciImageTrans.cgImage)//输出为:nil
        
  let cgImage = image?.cgImage
  let cgImageTrans = UIImage(cgImage: cgImage!)
  let cgdata = cgImageTrans.pngData()
  kLog(cgdata)//Optional(1841 bytes)
  kLog(cgImageTrans.ciImage)//输出为:nil

看的出来,同样图片资源,通过不同的方式初始化的UIImage转化为Data大小是不同的

优雅的使用UIImage

在前面我们通过定义全局方法进行的简单的简写,实际上依然不够美观

//MARK: - 获取图片
let kImageName = { imageString in
    UIImage(named: imageString)
}

//全局颜色
let kMainColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 0.2)

let kBoldFont = { num in
    UIFont.systemFont(ofSize: num)
}

如何更优雅的使用UIImage?

  1. 我们可以利用嵌套枚举来定义一些常用的只读计算属性来整理资源名、颜色、字体等项目常规设置
enum Global {
    enum image {
        static var working_table: UIImage { kImageName("working_报表")! }
        static var working_charge: UIImage { kImageName("working_充值")! }
    }
    enum font {
        static var regularFont14: UIFont { kSystemFont(14) }
        static var boldFont14: UIFont { kBoldFont(14) }
        
        static func regularFont(_ size: CGFloat) -> UIFont{
            kSystemFont(size)
        }
        static func boldFont(_ size: CGFloat) -> UIFont{
            kBoldFont(size)
        }
    }
    enum color {
        static var maincolor: UIColor{
            kHexColorWithString("444444")
        }
        
        static func RandomColor() -> UIColor{
            kRandomColor()
        }
        
        static func RGBColor(_ red:CGFloat, _ green:CGFloat, _ blue:CGFloat) -> UIColor{
            kRGBColor(red, green, blue)
        }
        
        static func RGBAColor(_ red:CGFloat, _ green:CGFloat, _ blue:CGFloat, _ alpha:CGFloat) -> UIColor{
            kRGBAColor(red, green, blue, alpha)
        }
        
    }
}

调用代码如下:

  var image = Global.image.working_table

  label.font = Global.font.regularFont(18)
  label.textColor = Global.color.RandomColor()

  button.titleLabel?.font = Global.font.boldFont14
  button.setTitleColor(Global.color.maincolor, for: UIControl.State.normal)

从OC到Swift中对资源名管理有更优秀的思路如:R.swift与SwiftGen等

  1. 给UIImage添加扩展
typealias ZQAsset = UIImage.Asset

extension UIImage{
    enum Asset : String {
        /// 订单
        case working_order = "working_订单"
        /// 公告
        case working_notice = "working_公告"
        
        var image : UIImage {
            return UIImage(asset: self)
        }
    }
    
    convenience init?(asset: Asset) {
        self.init(named: asset.rawValue)
    }
}

调用代码:

var image = ZQAsset.working_order.image

UIImageView

初始化
init(frame: CGRect)//继承自UIView
init(image: UIImage?)
init(image: UIImage?, highlightedImage: UIImage?)
UIImageView常用属性
  1. 可以同时设置image及highlightedImage,根据isHighlighted切换显示的图片

2.添加帧动画

  imageV = UIImageView(frame: CGRect(x: 15, y: 100, width: 100, height: 100))
  var images = [UIImage]()
  for i in 1...80 {
    images.append(kImageName("1-\(i)")!)
  }
  imageV?.image = urlImage
  imageV?.animationDuration = 5.0
  imageV?.animationRepeatCount = 2
  imageV?.animationImages = images
  imageV?.startAnimating()
  view .addSubview(imageV!)
上一篇下一篇

猜你喜欢

热点阅读