使用drawInRect时UIImage Aspect Fit?

UIView提供了“方面适合”的内容模式。 不过,我已经UIView子类,并希望绘制一个UIImage使用drawInRect方面适合。 有没有办法让我不使用UIImageView访问该function?

这个math有点毛,但幸运的是,这是我之前准备的一个解决scheme:

- (UIImage *)imageScaledToSize:(CGSize)size { //create drawing context UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f); //draw [self drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)]; //capture resultant image UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } - (UIImage *)imageScaledToFitSize:(CGSize)size { //calculate rect CGFloat aspect = self.size.width / self.size.height; if (size.width / aspect <= size.height) { return [self imageScaledToSize:CGSizeMake(size.width, size.width / aspect)]; } else { return [self imageScaledToSize:CGSizeMake(size.height * aspect, size.height)]; } } 

第一个function等同于“缩放填充”,第二个function等同于“方面适合”。 这些方法是作为一个UIImage的类别,所以要从另一个类中使用它们,你需要通过传递图像作为第二个参数来调整它们(或者像UIImage一样在UIImage上做一个类)。

你必须画出所提供的矩形

迅速:

func AVMakeRect(aspectRatio: CGSize, insideRect boundingRect: CGRect) -> CGRect

Objective-C的:

 CGRect AVMakeRectWithAspectRatioInsideRect(CGSize aspectRatio, CGRect boundingRect); 

请参阅文档

纵横比保持不变,graphics居中。

它绝对适用于所有情况。

这是解决scheme

 CGSize imageSize = yourImage.size; CGSize viewSize = CGSizeMake(450, 340); // size in which you want to draw float hfactor = imageSize.width / viewSize.width; float vfactor = imageSize.height / viewSize.height; float factor = fmax(hfactor, vfactor); // Divide the size by the greater of the vertical or horizontal shrinkage factor float newWidth = imageSize.width / factor; float newHeight = imageSize.height / factor; CGRect newRect = CGRectMake(xOffset,yOffset, newWidth, newHeight); [image drawInRect:newRect]; 

– 礼貌https://stackoverflow.com/a/1703210

另一个方面适合的select

 -(CGSize ) getImageSize :(CGSize )imgViewSize andActualImageSize:(CGSize )actualImageSize { // imgViewSize = size of view in which image is to be drawn // actualImageSize = size of image which is to be drawn CGSize drawImageSize; if (actualImageSize.height > actualImageSize.width) { drawImageSize.height = imgViewSize.height; drawImageSize.width = actualImageSize.width/actualImageSize.height * imgViewSize.height; }else { drawImageSize.width = imgViewSize.width; drawImageSize.height = imgViewSize.width * actualImageSize.height / actualImageSize.width; } return drawImageSize; } 

代码为迅速3.0:

 func getAspectFitFrame(sizeImgView:CGSize, sizeImage:CGSize) -> CGRect{ let imageSize:CGSize = sizeImage let viewSize:CGSize = sizeImgView let hfactor : CGFloat = imageSize.width/viewSize.width let vfactor : CGFloat = imageSize.height/viewSize.height let factor : CGFloat = max(hfactor, vfactor) // Divide the size by the greater of the vertical or horizontal shrinkage factor let newWidth : CGFloat = imageSize.width / factor let newHeight : CGFloat = imageSize.height / factor var x:CGFloat = 0.0 var y:CGFloat = 0.0 if newWidth > newHeight{ y = (sizeImgView.height - newHeight)/2 } if newHeight > newWidth{ x = (sizeImgView.width - newWidth)/2 } let newRect:CGRect = CGRect(x: x, y: y, width: newWidth, height: newHeight) return newRect } 

这可能会帮助你,试试看吧

 UIImage *logoImage = [UIImage imageNamed:@"logo.png"]; float newHeight = ((logoImage.size.height / logoImage.size.width) * 100.0); CGRect newLogoImageRect = CGRectMake(0, 0, 80.0, newHeight); [logoImage drawInRect:newLogoImageRect]; 

指定您的最大高度,而不是100.0和最大宽度,而不是80.0

以下将让您确定要绘制的大小。指定纵横比和边界大小以适应。

CGSizeAspectFit&CGSizeAspectFill

这里有一个辅助CGSize扩展,你可能会觉得有用:

 extension CGSize { func sizeThatFitsSize(_ aSize: CGSize) -> CGSize { let width = min(self.width * aSize.height / self.height, aSize.width) return CGSize(width: width, height: self.height * width / self.width) } } 

然后在-drawInRect:执行一个会执行以下操作:

 let imageSizeThatFitsViewBounds = self.image.size.sizeThatFitsSize(self.bounds.size) let imageRectX = (self.bounds.size.width - imageSizeThatFitsViewBounds.width) / 2 let imageRectY = (self.bounds.size.height - imageSizeThatFitsViewBounds.height) / 2 let imageRect = CGRect(origin: CGPoint(x: imageRectX, y: imageRectY), size: imageSizeThatFitsViewBounds)