设置一个随机应用程序背景图像iOS 6

问题描述:

我想为我的应用程序设置一个随机背景图像,这样每次有人启动应用程序时都会显示一个新的背景图像。设置一个随机应用程序背景图像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工作... 任何想法? :)

试试这个:

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

,而不是委托类把这个代码在主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

我认为你缺少的东西在你的代码,而你是从图像设置背景颜色

请参阅[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

有你需要考虑两件事情。首先,您需要通过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答案后面设置背景到你的根视图控制器的视图。

我得到了我的工作,谢谢大家回复,谢谢大家!

的代码应该是:

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