UIImageView(图片)

2017-05-11  本文已影响54人  Alan龙马
    lazy var titleImg: UIImageView = {
        
        let imgView = UIImageView ()
        imgView.image = UIImage.init(named: "x")
        imgView.backgroundColor = UIColor.yellow
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action:#selector(clickImg))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()
    
        titleImg.snp.makeConstraints { (make) -> Void in
            make.width.equalTo(50)
            make.height.equalTo(50)
        }

    @objc func clickImg(_ sender:UITapGestureRecognizer)
    {
        
    }

图片适应

       //图片自适应,ImageView会有部分空白
        imgView.contentMode = .scaleAspectFit
        //去图片中间部分,图片太大的情况
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        //图片不会保持原来的比例
         imgView.contentMode = .scaleToFill
image.png
保存图片到沙盒
方便查看UIImage 的大小
//打印工程所在文件路径
print(NSHomeDirectory())
//获取本地缓存文件路径
let cachPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
//设置保存路径
let path = cachPath[0] + "/img.png"
//将图片转换为png图片数据
//let imgData = UIImagePNGRepresentation(img!)
    do {
     //写入到指定路径
      try data222?.write(to: URL(fileURLWithPath: path))
        }
   catch{
            
        }
ImageView 基础

https://www.jianshu.com/p/0ae8c8fe0dc6

iOS-图片裁剪

https://github.com/Yasic/DynamicClipImage
https://www.jianshu.com/p/0ddf4f7476aa
https://www.jianshu.com/p/d9ca82c1834c
https://github.com/Rogue24/JPImageresizerView

图片压缩到指定大小 200k 以下
图片分类 旋转,截取。。。。
上一篇下一篇

猜你喜欢

热点阅读