import UIKit
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
yeTyeImg()
}
func yeTyeImg(){
let imageView = UIImageView(frame: self.view.frame)
imageView.contentMode = .scaleAspectFit
self.view.addSubview(imageView)
let url = "https://images.apple.com/v/home/l/images/promos/watch-series-5/tile_aws5__fwphji1d8yeu_large.jpg"
let placeholder = UIImage(named: "background")!
imageView.image(url: url, placeholder: placeholder)
}
}
import UIKit
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
uiImageExtensions()
}
func uiImageExtensions()
{
let image = UIImage(named: "background")!
print("---图片的大小(字节数) SizeAsBytes: \(image.getSizeAsBytes())")
print("---图片的大小 SizeAsKilobytes: \(image.getSizeAsKilobytes()) Kb")
let newImage = UIImage(data: image.compressImage(rate: 0.5)!)!
print("---压缩后图片的大小: Compressed: \(newImage.getSizeAsKilobytes()) Kb")
let imageView = UIImageView(frame: self.view.bounds)
imageView.contentMode = .scaleAspectFit
imageView.image = newImage
self.view.addSubview(imageView)
}
func cropImage()
{
let image = UIImage(named: "Girl")!
print(image.size)
let croppedImage = image.croppedImage(CGRect(x: 140, y: 160, w: 200, h: 320))
print(croppedImage?.size as Any)
let imageView = UIImageView(frame: self.view.bounds)
imageView.contentMode = .center
imageView.image = croppedImage
self.view.addSubview(imageView)
}
}