更改方向更改时的UIImage视图

时间:2014-07-17 00:09:05

标签: objective-c cocoa-touch ios7 uiviewcontroller uiinterfaceorientation

我正在尝试在设备进入横向时更改UIImageView图像。我有定位工作,但对Objective-C来说相当新,并且发现很难改变图像。如果有人知道我做错了什么,并帮我解决这个问题,这将非常有帮助。谢谢你的时间。

设置 带导航控制器和WelcomeViewController的故事板

WelcomeViewController.h文件

#import <UIKit/UIKit.h>

@interface WelcomeViewController : UIViewController {
    IBOutlet UIImageView *imageToDisplay;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageToDisplay;

@end

WelcomeViewController.m文件

#import "WelcomeViewController.h"


@interface WelcomeViewController ()
@end


@implementation WelcomeViewController

@synthesize imageToDisplay;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation     
{
    UIDeviceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];

    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == 
UIInterfaceOrientationPortraitUpsideDown) {
        // removing @2x.png from imageNamed string worked
        imageToDisplay.image = [UIImage imageNamed:@"welcome@2x.png"];
    }

    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || 
toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        // removing @2x.png from imageNamed string worked
        imageToDisplay.image = [UIImage imageNamed:@"welcome-landscape@2x.png"];
    }
}

@end

1 个答案:

答案 0 :(得分:0)

发表评论

1)请勿对图像使用@ 2x后缀。 Cocoa根据设备自动加载正确的。

2)你有一个带有imageView的XIB或故事板吗?在这种情况下,您需要将其连接到imageToDisplay属性,而不是使用synthesize。

3)仔细检查您是否正确写入了图像名称,并确保将图像添加到项目中。请注意,iOS设备对文件名区分大小写。