调整UIImage大小会导致UIImage的数据大小增加

3
我正在尝试使用NYXImageKit调整UIImage的大小,但这会导致图像数据大小增加。
附注:原始图像的尺寸为1024 * 1024。
var originalImage = image; 
//originalImage SIZE 108 KB
var resizedImage = image.scaleToFillSize(CGSize(width: 256, height: 256)); 
//resizedImage BECAME 620 KB  

有什么想法吗?

以下代码来自NYXImageKit类UIImage+Resizing.m文件。

-(UIImage*)scaleToFillSize:(CGSize)newSize
{
    size_t destWidth = (size_t)(newSize.width * self.scale);
    size_t destHeight = (size_t)(newSize.height * self.scale);
    if (self.imageOrientation == UIImageOrientationLeft
        || self.imageOrientation == UIImageOrientationLeftMirrored
        || self.imageOrientation == UIImageOrientationRight
        || self.imageOrientation == UIImageOrientationRightMirrored)
    {
        size_t temp = destWidth;
        destWidth = destHeight;
        destHeight = temp;
    }

  /// Create an ARGB bitmap context
    CGContextRef bmContext = NYXCreateARGBBitmapContext(destWidth, destHeight, destWidth * kNyxNumberOfComponentsPerARBGPixel, NYXImageHasAlpha(self.CGImage));
    if (!bmContext)
        return nil;

    /// Image quality
    CGContextSetShouldAntialias(bmContext, true);
    CGContextSetAllowsAntialiasing(bmContext, true);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);

    /// Draw the image in the bitmap context

    UIGraphicsPushContext(bmContext);
  CGContextDrawImage(bmContext, CGRectMake(0.0f, 0.0f, destWidth, destHeight), self.CGImage);
    UIGraphicsPopContext();

    /// Create an image object from the context
    CGImageRef scaledImageRef = CGBitmapContextCreateImage(bmContext);
    UIImage* scaled = [UIImage imageWithCGImage:scaledImageRef scale:self.scale orientation:self.imageOrientation];

    /// Cleanup
    CGImageRelease(scaledImageRef);
    CGContextRelease(bmContext);

    return scaled;
}

请发布 scaleToFillSize(CGSize size) 的代码,并说明您使用的是哪种类型的图像? - Blind Ninja
请提供需要翻译的英文内容,我将尽力为您进行翻译。 - JayVDiyk
“Image quality” 的增加是原因。 - Blind Ninja
好的,请问我应该如何编辑代码,@HarvantS。 - JayVDiyk
我已经调整大小并压缩了一个大约240K的JPEG数据,然后重构为UIImage(data:),然后它的JPEG数据变成了大约600K!我考虑了点大小和像素大小,这些都是正确的。我认为UIImage(data:)实际上进行了一些平滑处理等操作,使图像数据变大了。 - zrfrank
2个回答

0

Swift 图像调整大小工具

import UIKit

func RBSquareImageTo(image: UIImage, size: CGSize) -> UIImage {
    return RBResizeImage(RBSquareImage(image), size)
}

func RBSquareImage(image: UIImage) -> UIImage {
    var originalWidth  = image.size.width
    var originalHeight = image.size.height

    var edge: CGFloat
    if originalWidth > originalHeight {
        edge = originalHeight
    } else {
        edge = originalWidth
    }

    var posX = (originalWidth  - edge) / 2.0
    var posY = (originalHeight - edge) / 2.0

    var cropSquare = CGRectMake(posX, posY, edge, edge)

    var imageRef = CGImageCreateWithImageInRect(image.CGImage, cropSquare);
    return UIImage(CGImage: imageRef, scale: UIScreen.mainScreen().scale, orientation: image.imageOrientation)
}

func RBResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size

    let widthRatio  = targetSize.width  / image.size.width
    let heightRatio = targetSize.height / image.size.height

    // Figure out what our orientation is, and use that to form the rectangle
    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
    } else {
        newSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRectMake(0, 0, newSize.width, newSize.height)

    // Actually do the resizing to the rect using the ImageContext stuff
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.drawInRect(rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

好的。让我试试,如果我得到其他解决方案,我会稍后通知您。 - Mehul

0

希望它能对你有所帮助,

public func resizeImage(width width: CGFloat, height: CGFloat) -> UIImage {

    let newSize: CGRect = CGRectMake(0, 0, width, height)
    UIGraphicsBeginImageContext(newSize.size)
    self.drawInRect(newSize)
    let resizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return resizedImage
}

谢谢回复,但是您提供的代码无法正确调整大小,导致质量非常糟糕。 - JayVDiyk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接