2011-10-04 50 views
112

我在許多文章中看到通過保持縱橫比來調整圖像的大小。這些功能在調整大小時使用RECT的固定點(寬度和高度)。但在我的項目中,我只需根據寬度調整視圖大小,高度應根據高寬比自動調整。 任何人都可以幫助我實現這一點。通過保持縱橫比和寬度來調整UIImage的尺寸

回答

215

Srikar的方法效果非常好,如果你知道這兩個高度寬度您的新尺寸。 例如,如果您只知道要縮放到的寬度,並且不關心您首先必須計算高度的比例因子的高度。

+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width 
{ 
    float oldWidth = sourceImage.size.width; 
    float scaleFactor = i_width/oldWidth; 

    float newHeight = sourceImage.size.height * scaleFactor; 
    float newWidth = oldWidth * scaleFactor; 

    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)); 
    [sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
+20

儘管這裏你確實不需要newWidth變量,但它已經是'i_width'了。 –

+2

您還需要按高度進行劃分,並使用較接近0的比例來比較比例因子。上面的代碼只適用於比高度更寬的圖片 – IceForge

+1

如果我想要,我只需要按高度進行劃分通過縮放寬度或高度來了解我的身高比。但由於TO明確要求保持比例和寬度,而不是寬度或高度,我的回答是完全正確的。 – Maverick1st

8

最簡單的方法是設置您的UIImageView的框架並將contentMode設置爲其中一個調整大小選項。

的代碼是這樣的 - 這可以作爲一個實用的方法 -

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize 
{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
+0

我設法讓它只用設置'contentMode'。 –

54

如果你不知道發生如果圖像將是縱向還是橫向(例如用戶需要與攝像頭PIC),我創建了另一個是需要最大寬度和高度參數的方法。

比方說你有一個UIImage *myLargeImage這是一個4:3的比例。

UIImage *myResizedImage = [ImageUtilities imageWithImage:myLargeImage 
             scaledToMaxWidth:1024 
               maxHeight:1024]; 

調整大小的UIImage將是1024x768如果風景;如果是肖像,則爲768x1024。這種方法也會生成更高分辨率的視網膜顯示圖像。

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)size { 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]); 
    } else { 
     UIGraphicsBeginImageContext(size); 
    } 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

+ (UIImage *)imageWithImage:(UIImage *)image scaledToMaxWidth:(CGFloat)width maxHeight:(CGFloat)height { 
    CGFloat oldWidth = image.size.width; 
    CGFloat oldHeight = image.size.height; 

    CGFloat scaleFactor = (oldWidth > oldHeight) ? width/oldWidth : height/oldHeight; 

    CGFloat newHeight = oldHeight * scaleFactor; 
    CGFloat newWidth = oldWidth * scaleFactor; 
    CGSize newSize = CGSizeMake(newWidth, newHeight); 

    return [ImageUtilities imageWithImage:image scaledToSize:newSize]; 
} 
+0

謝謝。這正是我一直在尋找的! – SeanK

+0

非常感謝! – 4GetFullOf

+1

這是最好的東西,但它總是返回我定義大小的兩倍 – Mashhadi

4

只需導入AVFoundation和使用AVMakeRectWithAspectRatioInsideRect(CGRectCurrentSize, CGRectMake(0, 0, YOUR_WIDTH, CGFLOAT_MAX)

2

爲了提高對瑞安的答案:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)size { 
CGFloat oldWidth = image.size.width; 
     CGFloat oldHeight = image.size.height; 

//You may need to take some retina adjustments into consideration here 
     CGFloat scaleFactor = (oldWidth > oldHeight) ? width/oldWidth : height/oldHeight; 

return [UIImage imageWithCGImage:image.CGImage scale:scaleFactor orientation:UIImageOrientationUp]; 
    } 
31

感謝@ Maverick1st的算法,我實現它Swift,在我的情況下,高度是輸入參數

class func resizeImage(image: UIImage, newHeight: CGFloat) -> UIImage { 

    let scale = newHeight/image.size.height 
    let newWidth = image.size.width * scale 
    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)) 
    image.drawInRect(CGRectMake(0, 0, newWidth, newHeight)) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage 
} 
15

此方法是UIImage上的一個類別。使用AVFoundation進行縮放以適應幾行代碼。 不要忘記導入#import <AVFoundation/AVFoundation.h>

@implementation UIImage (Helper) 

- (UIImage *)imageScaledToFitToSize:(CGSize)size 
{ 
    CGRect scaledRect = AVMakeRectWithAspectRatioInsideRect(self.size, CGRectMake(0, 0, size.width, size.height)); 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0); 
    [self drawInRect:scaledRect]; 
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return scaledImage; 
} 

@end 
+2

發現這個以最小的內存佔用給我最好的結果。 +1 –

+1

非常棒,非常感謝。 –

1

瑞安的解決方案@Ryan 在SWIFT代碼

使用:

func imageWithSize(image: UIImage,size: CGSize)->UIImage{ 
    if UIScreen.mainScreen().respondsToSelector("scale"){ 
     UIGraphicsBeginImageContextWithOptions(size,false,UIScreen.mainScreen().scale); 
    } 
    else 
    { 
     UIGraphicsBeginImageContext(size); 
    } 

    image.drawInRect(CGRectMake(0, 0, size.width, size.height)); 
    var newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

//Summon this function VVV 
func resizeImageWithAspect(image: UIImage,scaledToMaxWidth width:CGFloat,maxHeight height :CGFloat)->UIImage 
{ 
    let oldWidth = image.size.width; 
    let oldHeight = image.size.height; 

    let scaleFactor = (oldWidth > oldHeight) ? width/oldWidth : height/oldHeight; 

    let newHeight = oldHeight * scaleFactor; 
    let newWidth = oldWidth * scaleFactor; 
    let newSize = CGSizeMake(newWidth, newHeight); 

    return imageWithSize(image, size: newSize); 
} 
5

好了,我們這裏有用於調整圖像幾個很好的答案,但我只是修改按我的需要。希望這能幫助像我這樣的人。

我的要求是

  1. 如果圖像寬度大於1920,與1920的寬度和高度保持與原始的寬高比調整其大小。
  2. 如果圖像高度超過1080,請使用1080高度調整其大小,並使用原始高寬比保持寬度。

if (originalImage.size.width > 1920) 
{ 
     CGSize newSize; 
     newSize.width = 1920; 
     newSize.height = (1920 * originalImage.size.height)/originalImage.size.width; 
     originalImage = [ProfileEditExperienceViewController imageWithImage:originalImage scaledToSize:newSize];   
} 

if (originalImage.size.height > 1080) 
{ 
      CGSize newSize; 
      newSize.width = (1080 * originalImage.size.width)/originalImage.size.height; 
      newSize.height = 1080; 
      originalImage = [ProfileEditExperienceViewController imageWithImage:originalImage scaledToSize:newSize]; 

} 

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize 
{ 
     UIGraphicsBeginImageContext(newSize); 
     [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return newImage; 
} 

感謝@Srikar Appal,調整大小我用他的方法。

您可能還想檢查this以及調整大小的計算。

0

我有一個更簡單的方法

UIImage *placeholder = [UIImage imageNamed:@"OriginalImage.png"]; 
self.yourImageview.image = [UIImage imageWithCGImage:[placeholder CGImage] scale:(placeholder.scale * 1.5) 
            orientation:(placeholder.imageOrientation)]; 

乘數規模將定義圖像的scalling,更乘數越小,圖像解決它。所以你可以檢查什麼適合你的屏幕。

或者你也可以通過分割圖像寬度/屏幕寬度來獲得多倍。

32

最佳答案特立獨行1日的正確翻譯爲斯威夫特(帶最新的SWIFT 3工作):

func imageWithImage (sourceImage:UIImage, scaledToWidth: CGFloat) -> UIImage { 
    let oldWidth = sourceImage.size.width 
    let scaleFactor = scaledToWidth/oldWidth 

    let newHeight = sourceImage.size.height * scaleFactor 
    let newWidth = oldWidth * scaleFactor 

    UIGraphicsBeginImageContext(CGSize(width:newWidth, height:newHeight)) 
    sourceImage.draw(in: CGRect(x:0, y:0, width:newWidth, height:newHeight)) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return newImage! 
} 
5

編程:

imageView.contentMode = UIViewContentModeScaleAspectFit; 

故事板:

enter image description here

0

In Swift 3有一些變化。 這裏是UIImage的擴展:

public extension UIImage { 
    public func resize(height: CGFloat) -> UIImage? { 
     let scale = height/self.size.height 
     let width = self.size.width * scale 
     UIGraphicsBeginImageContext(CGSize(width: width, height: height)) 
     self.draw(in: CGRect(x:0, y:0, width:width, height:height)) 
     let resultImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return resultImage 
    } 
} 
0

這對我來說是完美的。保持寬高比並需要maxLength。寬度或高度不會超過maxLength

-(UIImage*)imageWithImage: (UIImage*) sourceImage maxLength: (float) maxLength 
{ 
    CGFloat scaleFactor = maxLength/MAX(sourceImage.size.width, sourceImage.size.height); 

    float newHeight = sourceImage.size.height * scaleFactor; 
    float newWidth = sourceImage.size.width * scaleFactor; 

    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)); 
    [sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
相關問題