1

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

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

我没有错误,但我不工作......有什么想法吗?:)

4

5 回答 5

1

而不是委托类将此代码放在您的主视图控制器的 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;
}
于 2013-02-16T12:43:48.023 回答
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];
}
于 2013-02-16T12:53:03.903 回答
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];

另一件事是,当 iOS 实例化主故事板时,它会为您做一些事情。其中之一是分配和初始化self.window属性,但它还实例化包含在故事板中的主视图控制器并将其分配给UIWindow'rootViewController属性。这意味着如果您的主视图控制器创建一个UIView(几乎每个视图控制器都这样做),它的背景将在视觉上覆盖UIWindow背景,除非它被设置为透明。由于设置透明UIView可能不是您想要的,而且顺便说一句很尴尬,我建议按照@Dilip 的回答将背景设置为根视图控制器的视图。

于 2013-02-16T13:22:20.997 回答
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];
}
于 2013-02-16T12:45:01.170 回答
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;
}
于 2013-02-19T11:48:43.013 回答