gpt4 book ai didi

ios - 从风景或肖像 UIImage 中找到最大的中心正方形并将其缩放到一定大小

转载 作者:可可西里 更新时间:2023-11-01 06:19:00 26 4
gpt4 key购买 nike

我需要从缩放到一定尺寸的肖像或风景图像中找到最大的中心正方形。例如。如果我得到一张尺寸为 1200x800 的图像,我需要将居中的正方形缩小到 300x300。

最佳答案

我在 stackoverflow 上找到了这个问题的答案,该答案已被广泛复制。但是这个答案是不正确的,所以要发布正确的答案如下:

+ (UIImage*) cropBiggestCenteredSquareImageFromImage:(UIImage*)image withSide:(CGFloat)side
{
// Get size of current image
CGSize size = [image size];
if( size.width == size.height && size.width == side){
return image;
}

CGSize newSize = CGSizeMake(side, side);
double ratio;
double delta;
CGPoint offset;

//make a new square size, that is the resized imaged width
CGSize sz = CGSizeMake(newSize.width, newSize.width);

//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (image.size.width > image.size.height) {
ratio = newSize.height / image.size.height;
delta = ratio*(image.size.width - image.size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = newSize.width / image.size.width;
delta = ratio*(image.size.height - image.size.width);
offset = CGPointMake(0, delta/2);
}

//make the final clipping rect based on the calculated values
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * image.size.width),
(ratio * image.size.height));

//start a new context, with scale factor 0.0 so retina displays get
//high quality image
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
} else {
UIGraphicsBeginImageContext(sz);
}
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

我之前找到的错误答案如下:

+ (UIImage*) cropBiggestCenteredSquareImageFromImage:(UIImage*)image withSide:(CGFloat)side
{
// Get size of current image
CGSize size = [image size];
if( size.width == size.height && size.width == side){
return image;
}

CGSize newSize = CGSizeMake(side, side);
double ratio;
double delta;
CGPoint offset;

//make a new square size, that is the resized imaged width
CGSize sz = CGSizeMake(newSize.width, newSize.width);

//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (image.size.width > image.size.height) {
ratio = newSize.width / image.size.width;
delta = (ratio*image.size.width - ratio*image.size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = newSize.width / image.size.height;
delta = (ratio*image.size.height - ratio*image.size.width);
offset = CGPointMake(0, delta/2);
}

//make the final clipping rect based on the calculated values
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * image.size.width) + delta,
(ratio * image.size.height) + delta);


//start a new context, with scale factor 0.0 so retina displays get
//high quality image
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
} else {
UIGraphicsBeginImageContext(sz);
}
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

此代码的问题在于它无法正确裁剪。

两种代码都可以在下图中尝试: https://s3.amazonaws.com/anandprakash/ImageWithPixelGrid.jpg

正确的算法在上面的基本 url 上生成以下图像: https://s3.amazonaws.com/anandprakash/ScreenshotCorrectAlgo.png

错误的算法在上面的基本 url 上生成了以下图像 - 注意每边宽度上额外的 50px。 https://s3.amazonaws.com/anandprakash/ScreenshotWrongAlgo.png

关于ios - 从风景或肖像 UIImage 中找到最大的中心正方形并将其缩放到一定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14917770/

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