0

我正在处理应用程序的图像查看部分,我正在寻找一种调整图像大小或使其适合屏幕的方法。我的设置目前看起来像这样。

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];

这是我为segue方法做的准备

- (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];
    }
}

应该添加什么代码以使图像适合屏幕?

4

2 回答 2

2

在您的 UIImageView 中是 UIView 的子类。这允许您设置 UIViewContentMode 属性。您的选择是:

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

我相信您正在寻找 ScaleAspectFit。

于 2012-07-16T18:02:26.360 回答
0

制作一个与屏幕大小相同的 UIImageView。我假设你所有的照片都不是正确的比例不能被拉伸,所以我要做的是前面提到的 UIImageView (从现在开始我会调用它backImageView)并将它的背景设置为白色,或者任何颜色的背景你的图像。然后在你的view的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。

于 2012-07-16T18:02:57.047 回答