在UIViewContentModeScaleAspectFit之后获取已调整大小的图像的宽度

时间:2013-02-01 13:56:19

标签: iphone ios objective-c cocoa-touch uiimageview

我有我的UIImageView,我把图像放入其中,我按照这样调整大小:

UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.5, 245, 134)];
attachmentImageNew.image = actualImage;
attachmentImageNew.backgroundColor = [UIColor redColor];
attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit;

我尝试通过这样做来获取UIImageView中已调整大小的图片的宽度:

NSLog(@"Size of pic is %f", attachmentImageNew.image.size.width);

但实际上它会让我看到原始图片的宽度。关于如何获得我在屏幕上看到的图片框架的任何想法?

编辑:以下是我的UIImageView看起来的样子,红色区域是backgroundColor

enter image description here

6 个答案:

答案 0 :(得分:43)

我不知道有更明确的解决方案,但这有效:

float widthRatio = imageView.bounds.size.width / imageView.image.size.width;
float heightRatio = imageView.bounds.size.height / imageView.image.size.height;
float scale = MIN(widthRatio, heightRatio);
float imageWidth = scale * imageView.image.size.width;
float imageHeight = scale * imageView.image.size.height;

答案 1 :(得分:1)

Swift的解决方案:

let currentHeight = imageView.bounds.size.height
let currentWidth = imageView.bounds.size.width
let newWidth = UIScreen.mainScreen().bounds.width
let newHeight = (newWidth * currentHeight) / currentWidth

println(newHeight)

答案 2 :(得分:0)

它具有原始图像的参考,因此始终提供与原始图像相同的尺寸。

要获取新图像的尺寸,您必须检查纵横比。我使用预览知道如何根据其宽高比调整图像大小来为我的需要使用不同大小的不同图像。

答案 3 :(得分:0)

根据Apple的 UIViewContentModeScaleAspectFit

文档
  

通过维护内容(在您的情况下为actualImage)来缩放视图(在您的情况下为attachmentImageNew)的大小   宽高比。

这意味着您的图片尺寸(缩放后)应与UIImageView相同。

更新:

如果您不想使用UIViewContentModeScaleAspectFit,如果您可以修改scaledImage的大小,那么您可以使用以下代码缩放图像以修复newSize,然后再使用可以使用宽度代码。

CGSize newSize = CGSizeMake(100.0,50.0);
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 4 :(得分:0)

在你的情况下:

float yourScaledImageWitdh =  attachmentImageNew.frame.size.height *  attachmentImageNew.image.size.width / attachmentImageNew.image.size.height
NSLog(@"width of resized pic is %f", yourScaledImageWitdh);

但你还应该检查原始图像比例与imageView比例之前,我的代码行是好的,以防万一水平添加红色区域,以防万一你的原始图像比例比imageView帧比例宽

答案 5 :(得分:0)

对于 Swift 2 ,您可以使用此代码段将 aspectFitted 图片与屏幕的底部对齐(从此问题的早期答案中抽样 - >感谢你们,伙计们)

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = CGFloat(screenSize.width)
let screenHeight = CGFloat(screenSize.height)

imageView.frame = CGRectMake(0, screenHeight - (imageView.image?.size.height)! , screenWidth, (imageView.image?.size.height)!)

let newSize:CGSize = getScaledSizeOfImage(imageView.image!, toSize: self.view.frame.size)

imageView.frame = CGRectMake(0, screenHeight - (newSize.height) , screenWidth, newSize.height)

func getScaledSizeOfImage(image: UIImage, toSize: CGSize) -> CGSize       
{
    let widthRatio = toSize.width/image.size.width
    let heightRatio = toSize.height/image.size.height
    let scale = min(widthRatio, heightRatio)
    let imageWidth = scale*image.size.width
    let imageHeight = scale*image.size.height
    return CGSizeMake(imageWidth, imageHeight)
}