将图像裁剪为 iOS 中的正方形
Crop an image to a square in iOS
在我的应用程序中,我使用 UIImagePickerController
从相机或照片库导入图像。比我将导入的图像保存到应用程序文档目录中。这一切都很好,但是我想将裁剪后的图像保存为正方形(如在 Instagram 上)而不是原来的 size.The 正方形应该是图像宽度或高度的大小(取决于其中较小的那个)。我认为 CGRect 在这里可能会有用,但我不知道如何从图像中裁剪 CGRect。我看过无数教程,但其中 none 似乎有效,或者它们都太复杂了..
-(UIImage *)squareImage:(UIImage *)image
{
if (image.size.width>=image.size.height)
{
image=[self imageWithImage:image scaledToHeight:100];
}
else
{
image=[self imageWithImage:image scaledToWidth:100];
}
return image;
}
-(UIImage*)imageWithImage:(UIImage*)sourceImage scaledToWidth:(float)width
{
float oldWidth = sourceImage.size.width;
float scaleFactor = width / oldWidth;
float newHeight = sourceImage.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newWidth));
[sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
-(UIImage*)imageWithImage:(UIImage*)sourceImage scaledToHeight:(float)height
{
float oldHeight = sourceImage.size.height;
float scaleFactor = height / oldHeight;
float newWidth = sourceImage.size.width * scaleFactor;
float newHeight = oldHeight * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newHeight, newHeight));
[sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
以上方法将帮助您按比例缩放图像并按正方形缩放图像。对于旋转,您可以在 google.
上搜索
在我的应用程序中,我使用 UIImagePickerController
从相机或照片库导入图像。比我将导入的图像保存到应用程序文档目录中。这一切都很好,但是我想将裁剪后的图像保存为正方形(如在 Instagram 上)而不是原来的 size.The 正方形应该是图像宽度或高度的大小(取决于其中较小的那个)。我认为 CGRect 在这里可能会有用,但我不知道如何从图像中裁剪 CGRect。我看过无数教程,但其中 none 似乎有效,或者它们都太复杂了..
-(UIImage *)squareImage:(UIImage *)image
{
if (image.size.width>=image.size.height)
{
image=[self imageWithImage:image scaledToHeight:100];
}
else
{
image=[self imageWithImage:image scaledToWidth:100];
}
return image;
}
-(UIImage*)imageWithImage:(UIImage*)sourceImage scaledToWidth:(float)width
{
float oldWidth = sourceImage.size.width;
float scaleFactor = width / oldWidth;
float newHeight = sourceImage.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newWidth));
[sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
-(UIImage*)imageWithImage:(UIImage*)sourceImage scaledToHeight:(float)height
{
float oldHeight = sourceImage.size.height;
float scaleFactor = height / oldHeight;
float newWidth = sourceImage.size.width * scaleFactor;
float newHeight = oldHeight * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newHeight, newHeight));
[sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
以上方法将帮助您按比例缩放图像并按正方形缩放图像。对于旋转,您可以在 google.
上搜索