iOS 根据UIImage 修改UIImageView Frame (包括截取图片中间部分)
iOS UIImageView 根据需求调整frame
1、图片的宽和高不相等,截取图片的中间部分,截取的部分Size明确
2、图片的宽度要等于其父视图的类的宽度,然后根据宽度计算高度,保证
图片不变形,显示正常
3、图片的宽度或高度大于其父视图的类的宽的或高度,如果是宽度的问题,
就以其父视图的宽度为准,确定图片的显示宽度;如果是高度的问题,就以
其父视图的高度为准,确定图片的显示高度。
以上三种情况,具体示例依次如下:
第一种情况,调用方法:
- (void)imageLoadedFinish:(UrlImageView *)oldImageView image:(UIImage *)imageV skuInfo:(NSDictionary *)skuInfo
{
截取的目标尺寸
float imgW = (WIN_SIZE.width-40)/3;
//截取中间部分
oldImageView.image = [self squareImageFromImage:imageV scaledToSize:imgW];
}
---截图操作 //截取中间部分
- (UIImage *)squareImageFromImage:(UIImage *)image scaledToSize:(CGFloat)newSize
{
--缩放比例
CGAffineTransform scaleTransform;
CGPoint origin;
图片大小大于图片高度的情况
if (image.size.width > image.size.height)
{
//image原始高度为200,缩放image的高度为400pixels,所以缩放比率为2
计算缩放比例
CGFloat scaleRatio = newSize / image.size.height;
scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
//设置绘制原始图片的画笔坐标为CGPoint(-100, 0)pixels
计算画笔的X轴
origin = CGPointMake(-(image.size.width - image.size.height) / 2.0f, 0);
} else
{
图片的高度大于宽度的情况
CGFloat scaleRatio = newSize / image.size.width;
scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
计算画笔的Y轴
origin = CGPointMake(0, -(image.size.height - image.size.width) / 2.0f);
}
CGSize size = CGSizeMake(newSize, newSize);
//创建画板为(400x400)pixels
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
} else
{
UIGraphicsBeginImageContext(size);
}
CGContextRef context = UIGraphicsGetCurrentContext();
//将image原始图片(400x200)pixels缩放为(800x400)pixels
CGContextConcatCTM(context, scaleTransform);
//origin也会从原始(-100, 0)缩放到(-200, 0)
[image drawAtPoint:origin];
//获取缩放后剪切的image图片
image = UIGraphicsGetImageFromCurrentImageContext();
结束画板绘制
UIGraphicsEndImageContext();
return image;
}
第二种情况:
//等比例缩放
{ 得到当前视图的frame
CGSize selfSize = self.frame.size;
得到image的frame
CGSize imageSize = imageV.size;
得到imageView 的frame
CGRect imageVRect = self.imageView.frame;
确定imageView 的宽度
imageVRect.size.width = selfSize.width;
根据宽度计算imageView 的高度
imageVRect.size.height = imageVRect.size.width*imageSize.height/imageSize.width;
//计算x,y
imageVRect.origin.x = 0;
imageVRect.origin.y = 0;
self.imageView.frame = imageVRect;
CGRect selfRect = self.frame;
selfRect.size.width = imageVRect.size.width;
selfRect.size.height = imageVRect.size.height;
self.frame = selfRect;
}
第三种情况:
//等比例缩放
(void)imageLoadedFinish:(UrlImageView *)oldImageView image:(UIImage *)imageV skuInfo:(NSDictionary *)skuInfo
{
得到当前视图的frame
CGSize selfSize = self.frame.size;
得到当前Image的frame
CGSize imageSize = imageV.size;
得到当前ImageView 的frame
CGRect imageVRect = self.imageView.frame;
image的宽度大于当前视图的宽度
if(imageSize.width > selfSize.width)
{
根据宽度计算高度,确定宽度
imageVRect.size.height = selfSize.width * imageSize.height / imageSize.width;
imageVRect.size.width = selfSize.width;
}
image的高度大于当前视图的高度
if(imageVRect.size.height > selfSize.height)
{
根据高度计算宽度,确定宽度
imageVRect.size.width = selfSize.height * imageVRect.size.width / imageVRect.size.height;
imageVRect.size.height = selfSize.height;
}
//计算x,y
imageVRect.origin.x = (selfSize.width-imageVRect.size.width)/2;
imageVRect.origin.y = (selfSize.height-imageVRect.size.height)/2;
self.imageView.frame = imageVRect;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。