2012-07-16 110 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,设置图像将使图像自动填充框架。