因爲我確定我的答案已經存在,所以我會事先致歉,但在嘗試了很多建議之後,我似乎無法讓我的應用程序正常工作。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