꾸준히 안타치기
UIIMage 사이즈 줄이기 메소드 본문
반응형
https://stackoverflow.com/questions/29137488/how-do-i-resize-the-uiimage-to-reduce-upload-image-size
How do I resize the UIImage to reduce upload image size
I've been searching google, and have only come across libraries that either reduce the height/width or some how edit the UIImage appearance via CoreImage. But I have not seen or found one library, ...
stackoverflow.com
extension UIImage {
func resized(withPercentage percentage: CGFloat, isOpaque: Bool = true) -> UIImage? {
let canvas = CGSize(width: size.width * percentage, height: size.height * percentage)
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: canvas, format: format).image {
_ in draw(in: CGRect(origin: .zero, size: canvas))
}
}
func resized(toWidth width: CGFloat, isOpaque: Bool = true) -> UIImage? {
let canvas = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: canvas, format: format).image {
_ in draw(in: CGRect(origin: .zero, size: canvas))
}
}
}
Usage:
let image = UIImage(data: try! Data(contentsOf: URL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!))!
let thumb1 = image.resized(withPercentage: 0.1)
let thumb2 = image.resized(toWidth: 72.0)
반응형
'iOS > Basic Study' 카테고리의 다른 글
userDefault / 저장 (0) | 2022.08.11 |
---|---|
Delegation패턴을 이용한 커스텀쎌 버튼 클릭 (0) | 2022.05.19 |
Property List / CoreData - 데이터 저장방법 (0) | 2022.02.09 |
xcode 단축키 (0) | 2022.01.30 |
Notification / 이벤트 전달! /노티피케이션 센터와 노티피케이션 (0) | 2021.12.28 |
Comments