我试图在启动图像关闭后加载图像。所以我想在应用程序中添加一个imageview到self.window完成启动方法。我从缓存目录中选择图像。但它在加载前显示一个空白屏幕rootview控制器。 我该如何实现呢 这是我的代码,将图像添加到imageview,然后将图像视图添加到self.window作为子视图....
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@""];
imgView.frame=CGRectMake(0, 0, 768, 1024);
NSString *URLImg = [documentsDirectory stringByAppendingPathComponent:@"SplashIpad.png"];
UIImage *imgAvtar = [[UIImage alloc]initWithContentsOfFile:URLImg];
[imgView setImage:imgAvtar];
[self.window addSubview:imgView];
请建议我在启动画面关闭后如何加载图像。
答案 0 :(得分:4)
作为逻辑和代码,您可以这样做: -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[clsViewController alloc] initWithNibName:@"clsViewController" bundle:nil] autorelease];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@""];
NSLog(@"do ==%@",documentsDirectory);
NSString *URLImg = [documentsDirectory stringByAppendingPathComponent:@"index.jpg"];
NSLog(@"==%@",URLImg);
UIImage *imgAvtar = [[UIImage alloc]initWithContentsOfFile:URLImg];
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
[imgView setImage:imgAvtar];
imgView.center=self.viewController.view.center;
// UIImage *imgAvtar = [UIImage imageNamed:@"index.jpg"];
[self.viewController.view addSubview:imgView];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
<强> 强>
DEMO LINK:
在演示中,我从NSBundal获取图像,但您可以在didFinishLaunchingWithOptions
方法
http://www.sendspace.com/file/8srobj
注意: - 强>
确保/iPhone Simulator/6.0/Applications/8886B938-3EFB-4F0C-96F9-41ACAE508F2B/Library/Caches/index.jpg
路径中index.jpg
图片的名称为{{1}} palce in catch folder
答案 1 :(得分:0)
它可能对你有帮助。拿一个UIViewController并在其中拍摄一个图像视图(从缓存目录加载图像)。在appdelegate中将viewcontroller添加到窗口。如果你没有得到我,请告诉我。