声明UIImageView
let imageView=UIImageView(frame: CGRect(x: 140, y: 240, width: 100, height: 100))
let image = UIImage(named:"img3.jpeg")
imageView.image=image
view.addSubview(imageView)
设置图片缩放模式
- scaleAspectFit 在保持长宽比的前提下,缩放图片,使得图片在容器内完整显示出来。
- scaleAspectFill 在保持长宽比的前提下,缩放图片,使图片充满容器。
- scaleToFill 缩放图片,使图片充满容器。图片未必保持长宽比例协调,有可能会拉伸至变形。
imageView.contentMode = UIView.ContentMode.scaleAspectFit
加载本地图片
let imageView=UIImageView(frame: CGRect(x: 140, y: 240, width: 100, height: 100))
let image = UIImage(named:"img3.jpeg")
imageView.image=image
imageView.layer.borderWidth=5
imageView.layer.borderColor=UIColor.orange.cgColor
imageView.contentMode = UIView.ContentMode.scaleAspectFit
view.addSubview(imageView)
使用R.Swift加载本地图片
let imageView=UIImageView(frame: CGRect(x: 140, y: 240, width: 100, height: 100))
imageView.image=R.image.img1()
view.addSubview(imageView)
加载网络图片
-记得添加网络访问权限
在info.plist文件中添加网络访问权限,右键info.plist-->Open As-->Source Code
添加如下代码在适当位置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- 同步加载网络图片
let imageView=UIImageView(frame: CGRect(x: 140, y: 240, width: 100, height: 100))
//定义URL对象
let url = URL(string: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2727960467,599433451&fm=26&gp=0.jpg")
//从网络获取数据流
let data = try! Data(contentsOf: url!)
//通过数据流初始化图片
let newImage = UIImage(data: data)
imageView.image=newImage
view.addSubview(imageView)
- 异步加载网络图片
首先我们给UIImageView创建扩展函数,dowloadFromServer,方便使用
extension UIImageView {
func dowloadFromServer(url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() {
self.image = image
}
}.resume()
}
func dowloadFromServer(link: String, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
dowloadFromServer(url: url, contentMode: mode)
}
}
开始加载图片
let imageV = UIImageView(frame: CGRect(x: 140, y: 360, width: 100, height: 100))
imageV.layer.borderWidth = 5
imageV.layer.borderColor = UIColor.red.cgColor
imageV.dowloadFromServer(link:
"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1436766179,2996211296&fm=26&gp=0.jpg",
contentMode: .scaleAspectFill)
view.addSubview(imageV)
使用imageView实现图片动画
let imgSource=[R.image.img1(),R.image.img2(),R.image.img3(),R.image.img2(),R.image.img3()];
let imageView=UIImageView(frame: CGRect(x: 140, y: 240, width: 100, height: 100))
var imgArray=Array<UIImage>()
for item in imgSource {
imgArray.append(item! as UIImage)
}
imageView.animationImages=imgArray//图片列表
imageView.animationDuration=3//动画持续时间
imageView.animationRepeatCount=2//重复执行次数
imageView.startAnimating()//开始动画
view.addSubview(imageView)
关于UIImageView基本的使用就这些,注意在加载网络图片的时候,一定要添加网络连接的相关配置,更多场景以后继续补充。
网友评论