gpt4 book ai didi

iphone - UIImage 奇怪的缩放

转载 作者:行者123 更新时间:2023-11-28 20:22:58 25 4
gpt4 key购买 nike

我有一些文件大小(例如 1000x1000)。

我要加载它,让它变小

UIImage *newImage = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:filename].CGImage scale:(1.0f / scale) orientation:UIImageOrientationUp];
NSLog(newImage size: %f, %f", newImage.size.width, newImage.size.height);
// shows correct scaled size -> 50, 50

然后我想保存这张缩放过的图片

NSData* imageData = UIImageJPEGRepresentation(newImage, 1.0);
[imageData writeToFile:otherFileName atomically:NO];

看到新图像以第一个起始尺寸 (1000x1000) 保存时,我感到很困惑。

这怎么可能,怎么办?

最佳答案

只做一个方法:

-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {

float width = newSize.width;
float height = newSize.height;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect: CGRectMake(0, 0, width, height)];

UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return smallImage;
}

然后像这样设置你需要的尺寸:

UIImage *resizedImg = [self scaleImage:[UIImage imageWithData:imageData] toSize:CGSizeMake(150.0f,150.0f)]; // i have taken 150 you can change value here

关于iphone - UIImage 奇怪的缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15266610/

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