2012-03-05 53 views
-1

我正在研究新的應用程序,其中,我有不同大小的圖像。我已經嘗試了許多次不同的解決方案,但無法找到適合圖像的長寬比。我也想顯示高質量的圖像。有些圖像的分辨率爲20 * 20到3456 * 4608,或者它可能非常高。調整適當的長寬比的圖像

所有圖像都會在不裁剪的情況下顯示,但我們的應用屏幕尺寸爲300 * 370 pxls。

我就是按照以下參考文獻:

1:Resize UIImage with aspect ratio?

2:UIViewContentModeScaleAspectFit

3:scale Image in an UIButton to AspectFit?

4:Resize UIImage with aspect ratio?

+0

你就不能讓你的屏幕比例寬度/高度,然後應用,爲你的形象,你必須顯示? – 2012-03-05 14:26:46

回答

0

,我使用的調整大小代碼圖像邊緣1000px最多是這樣的:

const int maxPhotoWidthOrHeight = 1000; 

- (UIImage*)resizeImageWithImage:(UIImage*)image 
{ 
    CGFloat oldWidth = image.size.width; 
    CGFloat oldHeight = image.size.height; 
    NSLog(@"Old width %f , Old Height : %f ",oldWidth,oldHeight); 

    if (oldHeight > maxPhotoWidthOrHeight || oldWidth > maxPhotoWidthOrHeight) {//resize if necessary 
     CGFloat newHeight; 
     CGFloat newWidth; 
     if (oldHeight > oldWidth) { 
      newHeight = maxPhotoWidthOrHeight; 
      newWidth = oldWidth * (newHeight/oldHeight); 
     } 
     else{ 
      newWidth = maxPhotoWidthOrHeight; 
      newHeight = oldHeight * (newWidth/oldWidth); 
     } 
     CGSize newSize = CGSizeMake(newWidth, newHeight); 
     UIGraphicsBeginImageContext(newSize); 
     [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     NSLog(@"New width %f , New Height : %f ",newImage.size.width,newImage.size.height); 

     return newImage; 
    } 
    else{//do not resize because both edges are less than 1000px 
     return image; 
    } 

}