Ios 如何将文档目录中的图像加载到Objective-C中的图像视图中
您应该尝试以下代码从文档目录保存和检索图像: 我已经在AppDelegate.m中实现了这些方法Ios 如何将文档目录中的图像加载到Objective-C中的图像视图中,ios,objective-c,Ios,Objective C,您应该尝试以下代码从文档目录保存和检索图像: 我已经在AppDelegate.m中实现了这些方法 NSString *path = [NSString stringWithFormat:@"%@/%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0], parentFolderName] ; NSArray *dirContents = [[NSFil
NSString *path = [NSString stringWithFormat:@"%@/%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0], parentFolderName] ;
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSEnumerator *enumerator = [dirContents objectEnumerator] ;
id fileName ;
NSMutableArray *fileArray = [[NSMutableArray alloc] init] ;
while (fileName = [enumerator nextObject])
{
NSString *fullFilePath = [path stringByAppendingPathComponent:fileName];
NSRange textRangeJpg = [[fileName lowercaseString] rangeOfString:[@".png" lowercaseString]];
if (textRangeJpg.location != NSNotFound)
{
originalImage = [UIImage imageWithContentsOfFile:fullFilePath];
[fileArray addObject:originalImage];
}
}
@Sandy请尝试以下代码
-(NSString *)getDocumentDirectoryPath:(NSString *)Name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:Name];
NSLog(@"savedImagePath: %@", savedImagePath);
return savedImagePath;
}
-(BOOL)saveImage:(UIImage *)image withName:(NSString *)Name
{
NSData *imageData = UIImagePNGRepresentation(image);
BOOL success = [imageData writeToFile:[AppDelegate getDocumentDirectoryPath:Name] atomically:NO];
return success;
}
-(UIImage *)getRealtorImage:(NSString *)Name
{
UIImage *img = [UIImage imageWithContentsOfFile:[AppDelegate getDocumentDirectoryPath:Name]];
return img;
}
你面临的问题是什么?请提供您面临的问题的详细信息?图像未显示在图像视图中我正在获取图像数组列表我想要显示图像?您是否收到任何错误?您是否设置了断点并检查了
fullFilePath
?你在用fileArray
做什么?这只是一个数组,发布如何将图像添加到屏幕上的代码。while(文件名=[enumerator nextObject]){NSString*fullFilePath=[path stringByAppendingPathComponent:fileName];NSRange textRangeJpg=[[fileName lowercaseString]rangeOfString:[@.png“lowercaseString]”;如果(textRangeJpg.location!=NSNotFound){originalImage=[UIImage withcontentsoffile:fullFilePath];/[fileArray addObject:originalImage];_drawingView.backgroundColor=[uicolorWithpatternImage:[fileArray objectAtIndex:0];}}返回fileArray;格式化并将其放入问题中这样每个人都能正确地看到它。
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100,200,200)];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imageFilePath]];
if (imgData != nil)
{
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
imageView.image = thumbNail;
}
[self.view addSubView:imageView];