【发布时间】:2023-03-19 02:20:01
【问题描述】:
我是新手,这周搜索了很多。我正在尝试从我从 url 下载的文件中设置背景图像。我需要保存图像以备后用。特别是如果我没有连接到互联网,我仍然可以显示此图像。
我已通过打印出该文件存在的目录内容进行了验证。这是我尝试让图像出现在背景中的众多代码之一。
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];
}
代码在目录中找到图像,但我无法将图像设置为背景。请帮忙。
谢谢
【问题讨论】:
标签: iphone objective-c image ipad background