在iOS 6上设置随机应用程序背景图像
我想为我的应用程序设置一个随机背景图像,这样每当有人启动应用程序时,都会显示一个新的背景图像 以下是我迄今为止在在iOS 6上设置随机应用程序背景图像,ios,sdk,background,uiimageview,Ios,Sdk,Background,Uiimageview,我想为我的应用程序设置一个随机背景图像,这样每当有人启动应用程序时,都会显示一个新的背景图像 以下是我迄今为止在-(BOOL)应用程序中尝试的内容:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项 方法: 我没有错误,但我不工作。。。 有什么想法吗? :)将此代码放在主视图控件的initWithNibName方法中,而不是委托类: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBu
-(BOOL)应用程序中尝试的内容:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
方法:
我没有错误,但我不工作。。。
有什么想法吗?
:)将此代码放在主视图控件的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;
}
试试这个:
- (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];
}
我认为在设置图像的背景色时,代码中缺少了一些内容 请参见
[myImageNames objectAtIndex:index]
,此代码将返回just string对象
因为您只是将字符串
传递给需要图像的方法
所以你应该把这段代码改成这个
[UIImage ImageName:[myImageNames对象索引:索引]
现在,您的代码将如下所示
- (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];
}
有两件事需要考虑。首先,您需要通过<代码> UIImage <代码>,而不是<代码> NScord,以“模式NoSimule”<代码> > In。代码应该是:
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];
- (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;
}
另一件事是,当iOS实例化主情节提要时,它会为您做一些事情。其中之一是分配和初始化
self.window
属性,但它也实例化了包含在情节提要中的主视图控制器,并将其分配给UIWindow
的rootViewController
这意味着如果主视图控制器创建一个UIView
(几乎每个视图控制器都这样做),它的背景将在视觉上覆盖ui窗口
背景,除非它设置为透明。由于设置透明的ui视图
可能不是您想要的,顺便说一句,这很尴尬,我建议在@Dilip answer之后,将背景设置为根视图控制器的视图。我之所以能工作,是因为所有这些你们回答说,谢谢大家
代码应为:
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];
- (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;
}