2014-04-29 3 views
0

프로그래밍 방식으로 배경 이미지를로드하려고하지만 이미지가 표시되지 않습니다.UIImageView에서 이미지를로드하는 iOS가 표시되지 않습니다.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"]; 
     UIImage *backGroundImage = [UIImage imageWithContentsOfFile:filePath]; 
     self.backGroundView = [[UIImageView alloc] initWithImage:backGroundImage]; 
     self.backGroundView.hidden = NO; 

    } 
    return self; 
} 

콘솔에게 UIImageView에 인쇄하는 경우 : 여기 내 코드는

po _backGroundView 
<UIImageView: 0x15d40cf0; frame = (0 0; 568 320); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x15d41fb0>> 

이미지가 화면에로드되지 않는 이유 당신의 상관 알고 있습니까?

+0

가'뷰 계층에 추가되는 self.backGroundView' 있습니까? – Stonz2

+0

스토리 보드를 사용하고 있습니까? filePath가 유효합니까? [UIImage imageNamed : @ "xxx"]로로드하려고 했습니까? ? – jomafer

답변

3

기존의 backgroundView를 새로운 것으로 덮어 쓰고 있습니다. 기존의 backgroundView를 기존의 backgroundView로 설정하면됩니다 (펜촉이로드 될 때 자동으로 생성됩니다).

self.backGroundView.image = backGroundImage; 

코드는 순간 인 방법, 당신은 다음 viewController.view에 추가되지 않습니다 새와 펜촉 만들어보기를 교체합니다. 표시되지 않습니다. 기존보기가 뷰 계층 구조에 그대로 있지만 속성에 연결되지 않으므로 이미지가 변경되지 않습니다.

0

삭제해야합니다. 코드는 initWithNibName:bundle:에서 self.backGroundView 관련 및 viewDidLoad에 추가 :

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"]; 
    UIImage *backGroundImage = [UIImage imageWithContentsOfFile:filePath]; 
    self.backGroundView = [[UIImageView alloc] initWithImage:backGroundImage]; 
    self.backGroundView.hidden = NO; 
    [self.view addSubview:self.backGroundView]; 
} 
관련 문제