2013-02-16 2 views
1

내 애플리케이션에 임의의 배경 이미지를 설정하여 누군가가 앱을 실행할 때마다 새로운 배경 이미지가 표시되도록하고 싶습니다.임의 애플리케이션 배경 이미지 설정 iOS 6

Here's은 필자가 지금까지 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 방법의 시도 무엇을 : 나는 오류를 얻을

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil]; 
    int index = arc4random() % [myImageNames count]; 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[myImageNames objectAtIndex:index]]; 
    self.window.backgroundColor = background; 
} 

하지만 난 일 ... 어떤 아이디어 나던? 대신 주요 viewcontrller의 initWithNibName 방법이 코드를 넣어 위임 클래스의 :

답변

0

이 시도 이미지의 배경색 설정

을 참조하십시오.이 코드는 문자열 개체를 반환합니다.

image이 필요한 메소드에 string을 전달하기 때문에.

그래서이

[UIImage imageNamed:[myImageNames objectAtIndex:index]에 코드의 조각을 변경해야합니다.

이제 코드는 고려해야 할 두 가지가 있습니다이

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil]; 
    int index = arc4random() % [myImageNames count]; 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed: [myImageNames objectAtIndex:index]]]; 
    self.window.backgroundColor = background; 
    [background release]; 
} 
+0

이것이 효과가 있습니다! 고맙습니다. –

+0

환영합니다. 정답이면 아무 대답이나하십시오. – Guru

1

: 당신이 동안 당신이 당신의 코드에서 뭔가가 생각

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil]; 
    int index = arc4random() % [myImageNames count]; 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:[myImageNames objectAtIndex:index]]]; 
    self.window.backgroundColor = background; 
    [background release]; 
} 
+0

I'l이 그것을 밖으로 시도, 정말 감사합니다! –

1

:

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

     NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png",@"image4.png", @"image5.png",@"image6.png", nil]; 
     int index = arc4random() % [myImageNames count]; 

     self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[myImageNames objectAtIndex:index]]]; 
    } 
    return self; 
} 
+0

나는 그것을 지금, 고맙습니다. :) –

1

처럼 보일 것입니다. 먼저 UIImage이 아닌 NSStringinitWithPatternImage으로 전달해야합니다. 코드는 다음과 같아야합니다

NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil]; 
    int index = arc4random() % [myImageNames count]; 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed: [myImageNames objectAtIndex:index]]]; 
    self.window.backgroundColor = background; 
    [background release]; 

다른 것은 아이폰 OS 메인 스토리 보드를 인스턴스화 할 때, 당신을 위해 몇 가지 않는다는 것입니다. 이 중 하나는 self.window 속성을 할당하고 초기화하는 작업이지만 스토리 보드에 포함 된 기본보기 컨트롤러를 인스턴스화하고 UIWindowrootViewController 속성에 할당합니다. 즉, 기본보기 컨트롤러가 UIView (거의 모든보기 컨트롤러가 수행함)을 만들면 해당 배경이 투명하게 설정된 경우가 아니면 해당 배경이 UIWindow 배경을 시각적으로 덮어 씁니다. 투명 UIView 설정이 원하는대로되지 않을 수 있으며, 어색하다. @Dilip 응답에 따라 백그라운드를 루트보기 컨트롤러보기로 설정하는 것이 좋습니다.

0

나는 모두에게 감사의 말을 전했다.

코드가 있어야한다 :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.jpg", @"image2.jpg", @"image3.png", nil]; 
    int index = arc4random() % [myImageNames count]; 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:[myImageNames objectAtIndex:index]]]; 
    self.window.backgroundColor = background; 
    // Override point for customization after application launch. 
    return YES; 
}