如何在Objective-C中将图像适合屏幕大小

时间:2012-07-16 17:54:36

标签: objective-c xcode

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

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

应添加哪些代码才能使图像适合屏幕?

2 个答案:

答案 0 :(得分:2)

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

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

我相信您正在寻找ScaleAspectFit。

答案 1 :(得分: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,设置图像将使图像自动填充帧。