2011-11-19 110 views
0

我是新手,本周搜索了很多。我正在尝试从我从网址下载的文件设置背景图片。我需要保存图像以备后用。特别是如果我没有连接到互联网,我仍然可以显示这个图像。iPhone开发 - 为下载图像设置IPad背景图像

我已通过打印出文件所在目录的内容进行验证。这是我试图让图像出现在背景中的许多代码之一。

nNSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

NSString *documentPath = [docDir stringByAppendingFormat:@"/mainback.png"]; 

// If you go to the folder below, you will find those pictures 

NSLog(@"%@",documentPath); 



if ([[NSFileManager defaultManager]fileExistsAtPath:documentPath]) { 

    NSData *data = [NSData dataWithContentsOfFile:documentPath]; 

    UIImage *image = [[UIImage alloc] initWithData:data]; 

    NSLog(@"WHAT - %f,%f",image.size.width,image.size.height); 

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:documentPath]]; 

    self.view.backgroundColor = background; 

    [background release]; 

} 

代码找到目录中的图像,但我无法将图像设置为背景。请帮忙。

感谢

回答

0

使用UIImageView(苹果文件链接),而不是做你试图用的UIColor做。

这将是一个更直接,更少问题。

+0

我需要它作为背景图像,因为我必须在它的正面显示一个表单。我可以使用UIImageView来显示背景图像吗?没有在网上找到它的任何例子。谢谢你的帮助 – dogwasstar