はじめに
画像の解像度を下げる方法を調べたので記事にしておきます。
こんな感じになる
実装
import SwiftUI
struct ContentView: View {
private let image = UIImage(named: "sample")!
var body: some View {
if let image = reduceResolution(of: image, scaleFactor: 0.1) {
Image(uiImage: image)
.resizable()
.scaledToFit()
}
}
func reduceResolution(of image: UIImage, scaleFactor: CGFloat) -> UIImage? {
let newSize = CGSize(width: image.size.width * scaleFactor, height: image.size.height * scaleFactor)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: CGRect(origin: .zero, size: newSize))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
おわり
モザイクアプリとか作れそうですね