簡體   English   中英

iOS App:定向后調整圖像大小

[英]iOS App: Resizing Image after Orientation

我會向您道歉,因為我確定我的答案在那里,但是在嘗試了許多建議之后,我似乎無法正常使用我的應用。

基本上,我想以與屏幕整個寬度相同的比例顯示一個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”時,它會調整大小,但位於底部(有/無代碼)將其居中。 我認為這是個小問題,但是我已經進行了試驗和測試,時間超出了我妻子的承受能力(即無視“待辦事項清單”),沒有任何進展,我認為是時候尋求幫助了。 在此先感謝您的幫助。

要獲得新的寬度和高度,請使用以下命令:

- (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

}

好的,弄清楚我需要使它起作用。 不知道這是否是最好的方法,但是對於我的測試程序來說已經足夠了。 同樣,也不再需要自動調整大小。 如果其他人遇到相同的問題,代碼現在如下所示:

- (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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM