Last active
February 5, 2022 15:35
-
-
Save FarisAlbalawi/40b3edd4f0db00e0f534c76af5533590 to your computer and use it in GitHub Desktop.
This UIImage extension for resize image to be fit for WhatsApp sticker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This UIImage extension for resize image to be fit for WhatsApp sticker | |
// By Faris Albalawi | |
// Stickers must be exactly 512x512 pixels. | |
// Tray image must be exactly 96x96 pixels. | |
// TO MAKE TRY IMAGE | |
let tryayImage = yourImage!.StickerSize96() | |
// TO MAKE STICKER IMAGE | |
let stickerImage = yourImage!.StickerSize512() | |
extension UIImage { | |
func pngFrom() -> UIImage { | |
let imageData = self.pngData() | |
let imagePng = UIImage(data: imageData!) | |
return imagePng! | |
} | |
} | |
extension UIImage { | |
func resizeImage(targetSize: CGSize) -> UIImage { | |
let size = self.size | |
let widthRatio = targetSize.width / size.width | |
let heightRatio = targetSize.height / size.height | |
let newSize = widthRatio > heightRatio ? CGSize(width: size.width * heightRatio, height: size.height * heightRatio) : CGSize(width: size.width * widthRatio, height: size.height * widthRatio) | |
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height) | |
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) | |
self.draw(in: rect) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return newImage! | |
} | |
} | |
extension UIImage { | |
func imageWithInsets(insets: UIEdgeInsets) -> UIImage? { | |
UIGraphicsBeginImageContextWithOptions( | |
CGSize(width: self.size.width + insets.left + insets.right, | |
height: self.size.height + insets.top + insets.bottom), false, self.scale) | |
let _ = UIGraphicsGetCurrentContext() | |
let origin = CGPoint(x: insets.left, y: insets.top) | |
self.draw(at: origin) | |
let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return imageWithInsets | |
} | |
} | |
extension UIImage { | |
// sticker image 512 * 512 | |
func StickerSize512() -> UIImage? { | |
let imageView = UIImageView(image: imageWithInsets(insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))) | |
imageView.contentMode = .scaleAspectFit | |
imageView.frame = CGRect(x: 0, y: 0, width: 512, height: 512) | |
let image = imageView.asImage() | |
let img = image.pngFrom() | |
let newImage = img.resizeImage(targetSize: CGSize(width:512, height: 512)) | |
return newImage | |
} | |
// resize trayImage 96 * 96 | |
func StickerSize96() -> UIImage? { | |
let imageView = UIImageView(image: imageWithInsets(insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))) | |
imageView.contentMode = .scaleAspectFit | |
imageView.frame = CGRect(x: 0, y: 0, width: 96, height: 96) | |
let image = imageView.asImage() | |
let img = image.pngFrom() | |
let newImage = img.resizeImage(targetSize: CGSize(width:96, height: 96)) | |
return newImage | |
} | |
} | |
extension UIView { | |
func asImage() -> UIImage { | |
let renderer = UIGraphicsImageRenderer(bounds: bounds) | |
return renderer.image { rendererContext in | |
layer.render(in: rendererContext.cgContext) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment