因为我确定我的答案已经存在,所以我会事先致歉,但在尝试了很多建议之后,我似乎无法让我的应用程序正常工作。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