gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:39:36 24 4
gpt4 key购买 nike

我正在尝试使用 NYXImageKit 调整 UIImage 的大小,这会导致图像的数据大小增加

P.S.: originalImage 的尺寸是 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;
}

最佳答案

Swift Image Resizer

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
}

关于ios - 调整 UIImage 的大小会导致 UIImage 的数据大小增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32220315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com