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

时间:2013-02-16 12:33:38

标签: ios sdk background uiimageview

我想为我的应用程序设置一个随机背景图像,这样每次有人启动应用程序时都会显示一个新的背景图像。

这是迄今为止我在- (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;
}

我没有错,但我没有工作...... 有任何想法吗? :)

5 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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];
}

答案 2 :(得分: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属性,但它还实例化了故事板中包含的主视图控制器,并将其分配给UIWindow的{​​{1}}属性。这意味着如果您的主视图控制器创建rootViewController(几乎每个视图控制器都这样做),它的背景将在视觉上覆盖UIView背景,除非它被设置为透明。设置透明的UIWindow可能不是你想要的,顺便说一下,我建议在@Dilip回答之后将背景设置为根视图控制器的视图。

答案 3 :(得分: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];
}

答案 4 :(得分: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;
}