Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- guard let img = UIImage(named: "sampleImage") else {
- fatalError("Could not load image!!!!")
- }
- /// goal is to scale twice the PIXEL size
- if let scaledImage = imageCIScale(img, dimension: 2.0, outputScale: 1.0) {
- print("original image size:", img.size)
- print("scaled x 2.0 with 1.0 scale:", scaledImage.size)
- }
- if let scaledImage = imageCIScale(img, dimension: 2.0, outputScale: UIScreen.main.scale) {
- print("original image size:", img.size)
- print("scaled x 2.0 with Screen scale:", scaledImage.size)
- }
- }
- func imageCIScale(_ image: UIImage, dimension: CGFloat, outputScale: CGFloat) -> UIImage? {
- guard let ciImage = CIImage(image: image) else {
- print("no ciImage")
- return nil
- }
- guard let scaleFilter = CIFilter(name: "CILanczosScaleTransform") else {
- print("no filter")
- return nil
- }
- scaleFilter.setValue(ciImage, forKey: kCIInputImageKey)
- scaleFilter.setValue(ciImage, forKey: kCIInputImageKey)
- scaleFilter.setValue(dimension, forKey: kCIInputScaleKey)
- scaleFilter.setValue(1.0, forKey: kCIInputAspectRatioKey)
- guard let outputImage = scaleFilter.outputImage else { return nil }
- let uiImage = UIImage(ciImage: outputImage, scale: outputScale, orientation: image.imageOrientation)
- return uiImage
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement