简体   繁体   English

iPhone开发-从下载的图像为iPad设置背景图像

[英]IPhone Development - Setting Background Image For IPad From A Downloaded Image

I am new to this and have searched a lot this week. 我对此并不陌生,本周进行了很多搜索。 I am trying to set a background image from a file I have downloaded from a url. 我正在尝试从我从url下载的文件中设置背景图片。 I need to save the image for later. 我需要保存图像以备后用。 Specially if I am not connected to the internet I can still show this image. 特别是如果我没有连接到互联网,我仍然可以显示此图像。

I have validated by printing out the contents of the directory that the file exists. 我已经通过打印出文件所在目录的内容进行了验证。 This is one of many code that I have tried to make the image appear in the background. 这是我试图使图像显示在后台的众多代码之一。

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];

}

The code finds the image in the directory but I cannot set the image to the background. 代码在目录中找到图像,但是我无法将图像设置为背景。 Please help. 请帮忙。

Thanks 谢谢

Use a UIImageView (Apple documentation linked) instead of doing what you are trying to do with UIColor. 使用UIImageView (链接到Apple文档)而不要使用UIColor进行操作。

It'll be a lot more straightforward and less problematic. 这将更加简单明了,而不会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM