Objective c 从文档文件夹向Imageview添加图像未加载
我现在正试图解决一个相当简单的问题,但没有成功。我正在将文件保存到设备上的文档目录中,并尝试稍后使用图像视图加载它。我确认文件确实在那里。为什么我的图像没有显示出来 提前感谢你的帮助 下面是我尝试将图像加载到ImageView中的代码:Objective c 从文档文件夹向Imageview添加图像未加载,objective-c,ios,image,ipad,Objective C,Ios,Image,Ipad,我现在正试图解决一个相当简单的问题,但没有成功。我正在将文件保存到设备上的文档目录中,并尝试稍后使用图像视图加载它。我确认文件确实在那里。为什么我的图像没有显示出来 提前感谢你的帮助 下面是我尝试将图像加载到ImageView中的代码: -(void)loadFileFromDocumentFolder:(NSString *) filename { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirec
-(void)loadFileFromDocumentFolder:(NSString *) filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString: filename] ];
NSLog(@"outputPath: %@", outputPath);
UIImage *theImage = [UIImage new];
[UIImage imageWithContentsOfFile:outputPath];
if (theImage)
{
display = [UIImageView new];
display = [display initWithImage:theImage];
[self.view addSubview:display];
}
}
将代码更改为:
UIImage *theImage = [UIImage imageWithContentsOfFile:outputPath];
if (theImage)
{
display = [UIImageView alloc] initWithImage:theImage];
}
您的代码有一些问题
UIImage *theImage = [UIImage new];
在这一行中,您创建了一个新的UIImage
对象,但对它不做任何操作
[UIImage imageWithContentsOfFile:outputPath]
该类方法将返回一个带有从文件加载的图像的UIImage
对象
您可以使用UIImageView
执行相同的操作
NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString: filename] ];
也不需要[NSString stringWithString:filename]
您只需创建一个额外的字符串,因为filename
已经是一个字符串了
您的代码应该是这样工作的:
-(void)loadFileFromDocumentFolder:(NSString *) filename {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:filename ];
NSLog(@"outputPath: %@", outputPath);
UIImage *theImage = [UIImage imageWithContentsOfFile:outputPath];
if (theImage) {
display = [[UIImageView alloc] initWithImage:theImage];
[self.view addSubview:display];
}
}