2012-07-16 96 views
0

我正在研究應用程序的圖像查看部分,我正在尋找調整圖像大小或使其適合屏幕顯示的方法。我的設置目前看起來像這樣。如何在Objective-C中將圖像調整爲屏幕尺寸

photos = [[NSMutableArray alloc]init]; 

Photo *pic = [[Photo alloc]init]; 
[pic setName:@"Picture"]; 
[pic setFilename:@"Pic.jpeg"]; 
[pic setNotes:@"Description"]; 
[photos addObject:pic]; 

pic = [[Photo alloc]init]; 
[pic setName:@"Picture2"]; 
[pic setFilename:@"pic2.jpeg"]; 
[pic setNotes:@"Description2"]; 
[photos addObject:pic]; 

pic = [[Photo alloc]init]; 
[pic setName:@"Picture3"]; 
[pic setFilename:@"Pic3.jpeg"]; 
[pic setNotes:@"Description3"]; 
[photos addObject:pic]; 

pic = [[Photo alloc]init]; 
[pic setName:@"Picture4"]; 
[pic setFilename:@"Pic4.jpeg"]; 
[pic setNotes:@"Description4"]; 
[photos addObject:pic]; 

這裏是我的賽格瑞方法

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 

    if ([segue.identifier isEqualToString:@"ShowPhoto"]) 
    { 
     DisplayViewController *dvc = [segue destinationViewController]; 
     NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
     Photo *c = [photos objectAtIndex:path.row]; 
     [dvc setCurrentPhoto:c]; 
    } 
} 

什麼代碼應加,使圖像將適合屏幕準備?

+1

使用imageview屏幕的大小? – 2012-07-16 17:55:39

+0

我的意思是讓分辨率大於Iphone屏幕的照片變成letterbox,以便顯示整個圖像。 – Snorf 2012-07-16 17:58:41

+0

然後,您需要圖像查看屏幕的大小,並將'contentMode'設置爲'UIViewContentModeScaleAspectFit' – 2012-07-16 18:00:38

回答

2

在你的UIImageView中是UIView的一個子類。這使您可以設置UIViewContentMode屬性。你的選擇是:

typedef enum { 
    UIViewContentModeScaleToFill, 
    UIViewContentModeScaleAspectFit, 
    UIViewContentModeScaleAspectFill, 
    UIViewContentModeRedraw, 
    UIViewContentModeCenter, 
    UIViewContentModeTop, 
    UIViewContentModeBottom, 
    UIViewContentModeLeft, 
    UIViewContentModeRight, 
    UIViewContentModeTopLeft, 
    UIViewContentModeTopRight, 
    UIViewContentModeBottomLeft, 
    UIViewContentModeBottomRight, 
} UIViewContentMode; 

我相信你正在尋找ScaleAspectFit。

0

製作一個UIImageView,它是屏幕的大小。我假設你的所有照片都不是正確的比例,所以我會做的是有上述的UIImageView(從現在開始,我將它稱爲backImageView),並將其背景設置爲白色,或者任何顏色爲您的圖像的背景。然後,在視圖的viewDidLoad中,創建一個儘可能大的新UIImageView,同時尊重原始高寬比,然後將其放在新UIImageView的頂部。事情是這樣的:

double x = pic.size.width; 
double y = pic.size.height; 
if(x != 0 && y != 0){ 

    double ratio = x/y; 
    CGRect cellImageFrame = backImageView.frame; 
    UIImageView *actualPic = [[UIImageView alloc] init]; 

    if(y <= 320){ 
      if(x < 320){ 

       y = 320; 
       x = y*ratio; 
       double xoffset = cellImageFrame.size.width -x; 
       cellImageFrame = CGRectMake(cellImageFrame.origin.x + (xoffset/2), cellImageFrame.origin.y+2, x, y); 
      }else{ 
       x = 320; 
       y = x/ratio; 
       double yoffset = cellImageFrame.size.height -y; 
       cellImageFrame = CGRectMake(cellImageFrame.origin.x+2, cellImageFrame.origin.y + (yoffset/2), x, y); 

      } 
     }else{ 
      y = 320; 
      x = ratio*y; 
      if(x < 320){ 
       double xoffset = cellImageFrame.size.width -x; 
       cellImageFrame = CGRectMake(cellImageFrame.origin.x + (xoffset/2), cellImageFrame.origin.y+2, x, y); 
      }else{ 
       x = 320; 
       y = x/ratio; 

       double yoffset = cellImageFrame.size.height -y; 
       cellImageFrame = CGRectMake(cellImageFrame.origin.x+2, cellImageFrame.origin.y + (yoffset/2), x, y); 

      } 
     } 

     actualPic.image = pic; 
     actualPic.frame = cellImageFrame; 

這可能需要一些調整,我的代碼是不是全屏,我試圖修改它,但我不能測試它現在,所以我不能肯定。基本上你只是找出尺寸和寬高比,然後找出它是否比屏幕大。如果是,找出它是否比它更高,或者它是否方形,並相應地縮放。如果你想這樣做,則不會考慮縮放圖像,請檢查最小尺寸,然後使用縱橫比將其調高。一旦將框架設置爲所需的大小,只要具有UIViewContentModeScaleAspectFit,設置圖像將使圖像自動填充框架。