1

장치가 가로 방향으로 전환 될 때 UIImageView 이미지를 변경하려고합니다. 오리엔테이션이 작동하지만 Objective-C에 익숙하지 않고 이미지를 변경하기가 어렵습니다. 아무도 내가 뭘 잘못하고 있는지 알고 이것이 나를 고칠 수 있다면 정말 도움이 될 것입니다. 시간 내 줘서 고마워.방향 변경시 UIImage보기 변경

WelcomeViewController.h가 파일 탐색 컨트롤러와 WelcomeViewController와 스토리 보드를 설정

#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:@"[email protected]"]; 
    } 

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

@end 
+0

1) 이미지에'@ 2x '접미사를 사용하지 마십시오. Cocoa는 장치에 따라 자동으로 올바른 것을로드합니다. 2) * imageView *가있는 XIB 또는 스토리 보드가 있습니까? 이 경우, * synthesisize *를 사용하지 말고'imageToDisplay' 속성에 연결해야합니다. –

+0

1) 이미지 로딩에 대한 팁과 2) 내 UIImageView 구성 요소를 속성에 연결하는 것에 대해 지적 해 주셔서 감사합니다. UIImageView 구성 요소를 선택하고 컨트롤 드래그를 사용하여 @property (비 원자, 유지)에 연결합니다. IBOutlet UIImageView * imageToDisplay; 이제 장치를 회전하면 이미지가 사라지고 내 생각에 부적절한 구문이 생겼습니다. 나는 imageToDisplay.image = [UIImage imageNamed : @ "welcome-landscape.png"];를 사용하고 있습니다. 내 애플 리케이션 오리 엔테이션 변경에 검은 화면을두고있다 –

+0

이미지의 이름을 올바르게 쓰고 있는지 확인하고 이미지가 프로젝트에 추가되었는지 확인하십시오. iOS 장비는 파일 이름에 대해 대소 문자를 구분합니다. –

답변

0

내 댓글에 게시하기

1) 이미지에 @ 2x 접미사를 사용하지 마십시오. Cocoa는 장치에 따라 자동으로 올바른 것을로드합니다.

2) imageView가있는 XIB 또는 스토리 보드가 있습니까? 이 경우 synthesize를 사용하는 대신 imageToDisplay 속성에 연결해야합니다.

3) 이미지 이름을 올바르게 쓰는지 다시 확인하고 이미지가 프로젝트에 추가되었는지 확인하십시오. iOS 장비는 파일 이름에 대해 대소 문자를 구분합니다.