2013-02-16 128 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; 
} 

我沒有得到任何錯誤,但我doesn't工作... 任何想法? :)

回答

0

試試這個:

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

這工作!謝謝 – 2013-02-19 11:47:24

+0

welcome..accept任何一個答案,如果你有正確的答案。 – Guru 2013-02-19 11:53:23

1

,而不是委託類把這個代碼在主viewcontrller的initWithNibName方法:

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

I'l嘗試一下,太感謝你了! – 2013-02-19 11:00:11

1

我認爲你缺少的東西在你的代碼,而你是從圖像設置背景顏色

請參閱[myImageNames objectAtIndex:index],此代碼將返回正好的字符串對象。

因爲您只是將string傳遞給需要image的方法。

所以,你應該有一段代碼更改爲此

[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

我現在看到了,謝謝:) – 2013-02-19 11:03:47

1

有你需要考慮兩件事情。首先,您需要通過UIImage,而不是NSString,至initWithPatternImage。該代碼應該是:

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]; 

的另一件事是,當iOS的實例主要故事板,它做了幾件事情你。其中一件事是分配並初始化self.window屬性,但它也會實例化包含在故事板中的主視圖控制器,並將其分配給UIWindowrootViewController屬性。這意味着如果你的主視圖控制器創建了一個UIView(幾乎每個視圖控制器都會這樣做),它的背景將視覺覆蓋背景,除非它被設置爲透明。由於設置透明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; 
}