Ios 在UIView中缩放图像而不更改边界?
是否可以在不更改UIView边界的情况下缩放UIView中的图像?(也就是说,即使图像的比例大于UIView,仍将图像剪裁到UIView的边界。) 我在另一个SO post上发现了一些代码,可以在UIView中缩放图像:Ios 在UIView中缩放图像而不更改边界?,ios,uiview,clipping,Ios,Uiview,Clipping,是否可以在不更改UIView边界的情况下缩放UIView中的图像?(也就是说,即使图像的比例大于UIView,仍将图像剪裁到UIView的边界。) 我在另一个SO post上发现了一些代码,可以在UIView中缩放图像: view.transform = CGAffineTransformScale(CGAffineTransformIdentity, _scale, _scale); 但是,这似乎会影响视图的边界(使其变大),因此当其内容变大时,UIView的图形现在会在附近的其他UIVie
view.transform = CGAffineTransformScale(CGAffineTransformIdentity, _scale, _scale);
但是,这似乎会影响视图的边界(使其变大),因此当其内容变大时,UIView的图形现在会在附近的其他UIView上跺脚。在保持剪切边界不变的情况下,是否可以将其内容缩放得更大?缩放图像的最简单方法是通过设置其contentMode属性来使用UIImageView 如果必须使用UIView显示图像,可以尝试在UIView中重新绘制图像 1.UIView子类 2.在drawRect中绘制您的图像
//the followed code draw the origin size of the image
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[_yourImage drawAtPoint:CGPointMake(0,0)];
}
//if you want to draw as much as the size of the image, you should calculate the rect that the image draws into
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[_yourImage drawInRect:_rectToDraw];
}
- (void)setYourImage:(UIImage *)yourImage
{
_yourImage = yourImage;
CGFloat imageWidth = yourImage.size.width;
CGFloat imageHeight = yourImage.size.height;
CGFloat scaleW = imageWidth / self.bounds.size.width;
CGFloat scaleH = imageHeight / self.bounds.size.height;
CGFloat max = scaleW > scaleH ? scaleW : scaleH;
_rectToDraw = CGRectMake(0, 0, imageWidth * max, imageHeight * max);
}
缩放图像的最简单方法是通过设置其contentMode属性来使用UIImageView 如果必须使用UIView显示图像,可以尝试在UIView中重新绘制图像 1.UIView子类 2.在drawRect中绘制您的图像
//the followed code draw the origin size of the image
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[_yourImage drawAtPoint:CGPointMake(0,0)];
}
//if you want to draw as much as the size of the image, you should calculate the rect that the image draws into
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[_yourImage drawInRect:_rectToDraw];
}
- (void)setYourImage:(UIImage *)yourImage
{
_yourImage = yourImage;
CGFloat imageWidth = yourImage.size.width;
CGFloat imageHeight = yourImage.size.height;
CGFloat scaleW = imageWidth / self.bounds.size.width;
CGFloat scaleH = imageHeight / self.bounds.size.height;
CGFloat max = scaleW > scaleH ? scaleW : scaleH;
_rectToDraw = CGRectMake(0, 0, imageWidth * max, imageHeight * max);
}
除了@Jing的回答之外,为什么不把UIImageView放在容器视图中呢?我昨晚发现这是一种方法。您可以将父视图的“剪裁”设置为“是”,然后更改子视图上的变换,它就可以工作了!除了@Jing的回答之外,为什么不把UIImageView放在容器视图中呢?我昨晚发现这是一种方法。您可以将父视图的“剪裁”设置为“是”,然后更改子视图上的变换,它就可以工作了!将此标记为答案,因为您花了时间发布了回复,尽管昨晚我发现如果我创建UIImageView作为子视图,我可以在其上设置转换,然后对父视图调用setClipsToBounds(是),子视图将被剪裁。很好!是的,使用UIImageView是最简单的方法,我在第一行已经提到过,只需将UIImageView的contentMode设置为Aspect Fill即可达到效果,无需在超级视图中使用clipToBounds将其标记为答案,因为您花了时间发布回复,尽管昨晚我发现如果我将UIImageView创建为子视图,我可以在其上设置转换,然后对父视图调用setClipsToBounds并使用YES,然后剪裁子视图。很好!是的,使用UIImageView是最简单的方法,我在第一行已经提到过,只需将UIImageView的contentMode设置为Aspect Fill即可达到效果,无需在超级视图中使用clipToBounds