I\'m using the functions below to resize my images width & height but I noticed that it ruins the image quality.
class func imageWithSize(image: UIImage,
Here is my Objective-C
code. It's work for me.
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize isAspectRation:(BOOL)aspect {
if (!image) {
return nil;
}
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
CGFloat originRatio = image.size.width / image.size.height;
CGFloat newRatio = newSize.width / newSize.height;
CGSize sz;
if (!aspect) {
sz = newSize;
}else {
if (originRatio < newRatio) {
sz.height = newSize.height;
sz.width = newSize.height * originRatio;
}else {
sz.width = newSize.width;
sz.height = newSize.width / originRatio;
}
}
CGFloat scale = 1.0;
// if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
// CGFloat tmp = [[UIScreen mainScreen]scale];
// if (tmp > 1.5) {
// scale = 2.0;
// }
// }
sz.width /= scale;
sz.height /= scale;
UIGraphicsBeginImageContextWithOptions(sz, NO, scale);
[image drawInRect:CGRectMake(0, 0, sz.width, sz.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I saw your code in Swift
. I just convert my Objective-C
to Swift
without test. Can you try it and let's me know. Thanks!
struct CommonUtils {
static func imageWithImage(image: UIImage, scaleToSize newSize: CGSize, isAspectRation aspect: Bool) -> UIImage{
let originRatio = image.size.width / image.size.height;//CGFloat
let newRatio = newSize.width / newSize.height;
var sz: CGSize = CGSizeZero
if (!aspect) {
sz = newSize
}else {
if (originRatio < newRatio) {
sz.height = newSize.height
sz.width = newSize.height * originRatio
}else {
sz.width = newSize.width
sz.height = newSize.width / originRatio
}
}
let scale: CGFloat = 1.0
sz.width /= scale
sz.height /= scale
UIGraphicsBeginImageContextWithOptions(sz, false, scale)
image.drawInRect(CGRectMake(0, 0, sz.width, sz.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
I just created this useful UIImage extension with swift 5. You can use it to resize your UIImages without losing quality.
I added a couple of methods in order to be able to resize an image to a width or a height keeping the aspect ratio.
extension UIImage {
func resize(targetSize: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size:targetSize).image { _ in
self.draw(in: CGRect(origin: .zero, size: targetSize))
}
}
func resize(scaledToWidth desiredWidth: CGFloat) -> UIImage {
let oldWidth = size.width
let scaleFactor = desiredWidth / oldWidth
let newHeight = size.height * scaleFactor
let newWidth = oldWidth * scaleFactor
let newSize = CGSize(width: newWidth, height: newHeight)
return resize(targetSize: newSize)
}
func resize(scaledToHeight desiredHeight: CGFloat) -> UIImage {
let scaleFactor = desiredHeight / size.height
let newWidth = size.width * scaleFactor
let newSize = CGSize(width: newWidth, height: desiredHeight)
return resize(targetSize: newSize)
}
}
Here is my code in swift. For squared images, it works really well and without any loss of quality!
extension UIImage {
func resize(targetSize: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size:targetSize).image { _ in
self.draw(in: CGRect(origin: .zero, size: targetSize))
}
}
}
func image(image:UIImage,imageSize:CGSize)->UIImage
{
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)
[image .drawInRect(CGRectMake(0, 3, imageSize.width, imageSize.height))]
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
return newImage;
}