Iphone:如何在图像视图中显示文档目录图像?

时间:2011-07-12 11:30:19

标签: iphone objective-c cocoa-touch ios4

在文档目录文件夹中存储的应用程序图像中

我想在图像视图中显示特定的图像,

图像如何在文档目录的图像视图中显示?

7 个答案:

答案 0 :(得分:18)

示例代码:

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

-(void)loadimage{
    NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"your image-name"];
    UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)];
    myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];    
    [self.view addSubView:myimage];
    [myimage release];
}

TNQ

答案 1 :(得分:8)

您可以通过了解图像的名称来实现它,因此在存储图像时,您需要将这些图像的一些信息存储在数据库中(或者在某些地方可以使用它),然后从数据源中获取图像的名称,并按如下方式工作:

NSString *str = [NSString stringWithFormat:@"%@.jpg",[myGlobleArrayOfImageNames objectAtIndex:imageCounter]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullImgNm=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:str]];
[mainSlideShowImageView setImage:[UIImage imageWithContentsOfFile:fullImgNm]];

答案 2 :(得分:6)

在这里,您可以在视图中使用UIImageView并将其添加到视图中并执行相同操作,如下所示:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageFilePath;
NSError *err;

imageFilePath = [documentsDirectory stringByAppendingPathComponent:@"image1.png"];

 UIImageView *objImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
 objImageView.image = [UIImage imageWithContentsOfFile: imageFilePath];
 [self.view addSubVIew:objImageView.image];
 [objImageView release];

如果您仍需要任何帮助,请与我们联系。

答案 3 :(得分:4)

NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *imagePath =  [directoryPath objectAtIndex:0];
imagePath= [imagePath stringByAppendingPathComponent:@"imagename.png"];

 NSData *data = [NSData dataWithContentsOfFile:imagePath];
    UIImage *img = = [UIImage imageWithData:data];

您可以将此图像显示给uiimageview

答案 4 :(得分:1)

您需要以下代码行才能从文档目录中获取UIImage

-(UIImage*)getImage:(NSString *)strImageName
{
NSString *documentDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *imagePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",strImageName]];
UIImage *image=[UIImage imageWithContentsOfFile:imagePath];
return image;
}

调用上面的函数并使用UIImage对象设置你想要的任何地方。 您也可以在公共类中编写此方法,以便在任何地方使用它。

答案 5 :(得分:0)

添加到这些答案中,我找到了检查文件是否存在的良好做法......

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *file = [directory stringByAppendingPathComponent@"my_image.jpg"];
if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
    self.myimageview = [UIImage imageWithData:[NSData dataWithContentsOfFile: file]];

}

答案 6 :(得分:0)

简单使用此代码

NSFileManager *filemgr =[NSFileManager defaultManager];
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *dataPath = [docsDir stringByAppendingPathComponent:@"/yourdirectory/image.jpg"];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:dataPath]];
self.imageview.image = [[UIImage alloc] initWithData:imgData];