如何从iPhone中的文档目录中的特定文件夹中检索UIImage

时间:2012-05-18 06:37:14

标签: iphone uiimage

我使用以下函数将图像本地存储在文档目录

中创建的文件夹中
NSError *error;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];

// FOR STORING IMAGE INTO FOLDER CREATED IN DOCUMENT DIRECTORY

NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/ImagesFolder"];    
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

NSData* imageData = UIImagePNGRepresentation(imageView.image);
NSString* incrementedImgStr = [NSString stringWithFormat:@"Image%d.png",delegate.dirCountImages];
NSString* fullPathToFile2 = [dataPath stringByAppendingPathComponent:incrementedImgStr];
[imageData writeToFile:fullPathToFile2 atomically:NO];

但是,如何从文档目录

中的特定文件夹中检索图像

4 个答案:

答案 0 :(得分:0)

只需要拥有完整路径并使用此方法即可。 [[UIImage alloc]initWithContentsOfFile: <#path#>]如果您不使用ARC,请记得释放图像。

答案 1 :(得分:0)

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100,100,100)];
imageView.image = [[UIImage alloc] initWithData:myImage];

[myView addSubview:imageView];

或查看该链接http://www.iphonedevsdk.com/forum/iphone-sdk-development/23840-placing-image-url-image-view.html

答案 2 :(得分:0)

也许这会对你有帮助。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Images"];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Image.png"];
[cell.songImageView setImage:[UIImage imageWithContentsOfFile:documentsDirectory]];

答案 3 :(得分:0)

-(NSMutableArray*)getPhotoFileNames:(NSString*)parentFolderName
{
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) 
    {
        UIImage     *originalImage = [UIImage imageWithContentsOfFile:fullFilePath];
        [fileArray addObject:originalImage];
    }
}
return fileArray;
}