私は自分の答えがそこにあると確信しているので、事前に謝罪するつもりですが、多くの提案を試みた後、私のアプリが正しく動作するように見えません。iOSアプリケーション:オリエンテーション後の画像のサイズ変更
基本的には、画面の中央にある画面の全幅と同じ縮尺で16x9の画像を表示したいと考えています。ポートレート方向と比較して、画像は横長の方が大きくなります。私はイメージを表示することができますが、私のIpadをポートレートに回転させると、イメージの元の高さに保ちながらイメージの両端がクリップされます。私はレイアウトエディタを使用していないと私の最初のアプリのためにしたくないです。画像は次のコードを使用して読み込まれます。
viewController.h:
@interface ViewController : UIViewController
{
UIImageView* MobileImage; // image on mobile device
}
@property (nonatomic, retain) UIImageView* MobileImage;
@end
viewController.m:
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize MobileImage;
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat width = self.view.bounds.size.width;
CGFloat height = width * 9/16;
if (height > self.view.bounds.size.height) { // just in case
height = self.view.bounds.size.height;
width = height * 16/9;
}
self.MobileImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
self.MobileImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sandy-ralph" ofType:@"png"]];
self.MobileImage.center = self.view.center;
self.MobileImage.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.MobileImage.autoresizesSubviews = YES;
[self.view addSubview:self.MobileImage];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
私は(私が風景に始まり、肖像画に回転させると仮定した場合)クリッピングを気にしない場合、これは正常に動作します。私はそれのサイズを変更する私の試みでのViewControllerの実装に以下のコードを挿入します。
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
NSLog(@"Transition to size = %@", NSStringFromCGSize(size));
CGFloat width = size.width;
CGFloat height = width * 9/16;
if (height > size.height) { // just in case
height = size.height;
width = height * 16/9;
}
#if 1 // this code has no effect on the image size
NSLog(@"Using UIGraphicsGetImageFromCurrentImageContext");
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
[self.MobileImage.image drawInRect:CGRectMake(0, 0, width, height)];
UIGraphicsPopContext();
UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSUInteger i1 = [self.view.subviews indexOfObject:self.MobileImage];
UIView *superview = self.MobileImage.superview;
[self.MobileImage removeFromSuperview];
self.MobileImage.image = newimage;
[superview insertSubview:self.MobileImage atIndex:i1];
#else // the code resizes but causes image to sit on bottom of screen
NSLog(@"Using CGRectMake");
self.MobileImage.frame = CGRectMake(0.0, 0.0, width, height);
#if 1 // With/without next three lines, image still sits on bottom of screen
self.MobileImage.center = CGPointMake(width/2, height/2);
self.MobileImage.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.MobileImage.autoresizesSubviews = YES;
#endif
#endif
}
プリプロセッサディレクティブは、異なるコードをテストするためのものです。 "#if 1"では、サイズ変更はありません。 "#if 0"を指定すると、サイズ変更されますが、中央に配置するコードの有無にかかわらず、下部に配置されます。私はそれが小規模な修正だと思うが、私は妻が許すよりも長い間、試行錯誤していた(すなわち、 "リストをする"を無視する)、進歩もなく、助けを求める時間だと思った。助けを前にありがとう。この新しい幅と高さの使用を取得するには
私はすでに同じ名前の変数で新しい幅と高さを計算しました。画像はコードでこれらの値に正しくサイズ変更されています: self.MobileImage.frame = CGRectMake(0.0、0.0、width、height); それは私が立ち往生している新しい幅と高さで何をするかです。 – Codybear