iOS App:定位后调整图像大小

时间:2017-02-10 13:23:34

标签: ios objective-c

我会提前道歉,因为我确信我的答案已经存在,但在尝试了很多建议后,我似乎无法让我的应用程序正常工作。

基本上,我希望以相同比例显示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”,它会调整大小,但位于底部,有/无代码使其居中。我认为这是一个小修复,但我已经进行了试验和测试,比我妻子能够容忍的时间更长(即忽略“待办事项列表”),没有任何进展,我认为是时候寻求帮助了。提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

要获得新的宽度和高度,请使用:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    CGFloat screenWidth = size.width;
    CGFloat screenHeight = size.height;


    UIImage* image = self.MobileImage.image;
    CGFloat mobileImageWidth = image.size.width;
    CGFloat mobileImageHeight = image.size.height;

    CGFloat newMobileImageWidth = 0;
    CGFloat newMobileImageHeight = 0;

    double widthRatio = mobileImageWidth / screenWidth;
    double heightRatio = mobileImageHeight / screenHeight;

    if (widthRatio < heightRatio) {
        newMobileImageHeight = screenHeight;
        newMobileImageWidth = screenWidth / widthRatio;
    }
    else {
        newMobileImageWidth = screenWidth;
        newMobileImageHeight = screenHeight / heightRatio;
    }

    //use these new width and height for your image

}

答案 1 :(得分:0)

好的,弄清楚我需要做些什么才能让它发挥作用。不确定它是否是最佳方式,但它对我的测试程序来说已经足够了。此外,不再需要自动调整大小。如果其他人遇到同样的问题,代码现在看起来像:

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat viewheight = self.view.bounds.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height;
    CGFloat width = self.view.bounds.size.width;
    CGFloat height = width * 9 / 16;
    if (height > viewheight) {  // don't exceed viewable height
        height = viewheight;
        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.view addSubview:MobileImage];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    CGFloat statusbarheight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    CGFloat viewheight = size.height - statusbarheight;
    CGFloat width = size.width;
    CGFloat height = width * 9 / 16;
    if (height > viewheight) {  // don't exceed viewable height
        height = viewheight;
        width = height * 16 / 9;
    }

    CGRect frame = self.MobileImage.frame;
    frame.size.width = width;
    frame.size.height = height;
    frame.origin.x = (size.width - width) / 2;
    frame.origin.y = statusbarheight + (viewheight - height) / 2;
    self.MobileImage.frame = frame;
}
相关问题