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

そして、これがセグエメソッドの準備です

- (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) を使用し、背景を白、または背景の色に設定します。あなたのイメージの。次に、ビューの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 に答える