【发布时间】:2017-05-09 17:23:40
【问题描述】:
您好,我正在使用 UIimage,我需要将图像裁剪为特定尺寸,假设我的特定尺寸宽度等于设备宽度,高度固定为 200。 我在 Google 中尝试了很多示例,但没有一个不适合我,我尝试了以下示例
-(UIImage *)resizeImage:(UIImage *)image
{
float h=200;
float w=400;
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float maxHeight = h;
float maxWidth = w;
float imgRatio = actualWidth/actualHeight;
float maxRatio = maxWidth/maxHeight;
float compressionQuality = 0.8;//50 percent compression
if (actualHeight > maxHeight || actualWidth > maxWidth)
{
if(imgRatio < maxRatio)
{
//adjust width according to maxHeight
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
}
else if(imgRatio > maxRatio)
{
//adjust height according to maxWidth
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
}
else
{
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
//UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
UIGraphicsEndImageContext();
return [UIImage imageWithData:imageData];
}
下面是我的实物图
裁剪后我得到如下所示
我的所有示例都在拉伸图像但我想在没有拉伸的情况下缩放图像。我在哪里失踪?请帮助我。
【问题讨论】:
-
picker.allowsEditing = YES;当您捕获图像并当时选择图像时,设置一个属性并尝试放置
-
还有一个问题,只是将 imageview 属性设置为适合而不是 scall 来填充
-
我认为,这是您的尺寸问题,在调整图像大小后打印您的宽度和高度。我想你会得到一个矩形框架,这就是为什么图像是这样的
-
打印实际宽度和实际高度
-
只有当我从肖像模式横向模式正常工作时拍摄图像时才会出现问题
标签: ios iphone uiimage crop scaling