Skip to content

Instantly share code, notes, and snippets.

@tonycn
Created October 27, 2020 04:05
Show Gist options
  • Save tonycn/faafe16b10595ffa46881931c0d17deb to your computer and use it in GitHub Desktop.
Save tonycn/faafe16b10595ffa46881931c0d17deb to your computer and use it in GitHub Desktop.
import UIKit
import Photos
private func imageWithImage(originImage:UIImage, targetSize:CGSize, contentMode: PHImageContentMode) -> UIImage {
let originSize = CGSize(width: originImage.size.width * originImage.scale,
height: originImage.size.height * originImage.scale)
let originRatio = originImage.size.width / originImage.size.height
let targetSizeRatio = targetSize.width / targetSize.height
var newSize: CGSize;
if (targetSize.width >= originSize.width && targetSize.height >= originSize.height) {
return originImage;
}
if ((originRatio*10).rounded() == (targetSizeRatio*10).rounded()) {
if (targetSize.width > originSize.width) {
return originImage
} else {
newSize = targetSize
}
} else if (targetSizeRatio > originRatio && contentMode == .aspectFit) {
let height = min(originSize.height, targetSize.height)
newSize = CGSize.init(width: originRatio * height, height: height)
} else {
let width = min(originSize.width, targetSize.width)
newSize = CGSize.init(width: width, height: width / originRatio)
}
if #available(iOS 10.0, *) {
let renderer = UIGraphicsImageRenderer(size: newSize)
let newImage = renderer.image { _ in
originImage.draw(in: CGRect.init(origin: CGPoint.zero, size: newSize))
}
return newImage.withRenderingMode(originImage.renderingMode)
} else {
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
originImage.draw(in: CGRect.init(origin: .zero, size: newSize))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() ?? originImage
UIGraphicsEndImageContext()
return newImage
}
}
func test1() {
let img = UIImage.init(named: "IMG_3118_copy.jpg")
let newImage = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 50, height: 50), contentMode: .aspectFill);
let newImage1 = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 150, height: 50), contentMode: .aspectFill);
let newImage2 = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 5000, height: 5000), contentMode: .aspectFill);
let newImage3 = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 756/2, height: 1008/2), contentMode: .aspectFill);
let newImage4 = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 756*2, height: 1008*2), contentMode: .aspectFill);
}
test1()
func test2() {
let img = UIImage.init(named: "IMG_3118_copy.jpg")
let newImage = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 50, height: 50), contentMode: .aspectFit);
let newImage1 = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 150, height: 50), contentMode: .aspectFit);
let newImage2 = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 5000, height: 5000), contentMode: .aspectFit);
let newImage3 = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 756/2, height: 1008/2), contentMode: .aspectFit);
let newImage4 = imageWithImage(originImage: img!, targetSize: CGSize.init(width: 756*2, height: 1008*2), contentMode: .aspectFit);
}
test2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment