0

我已使用以下功能将图像本地存储在文档目录中创建的文件夹中

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

4 回答 4

0

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

于 2012-05-18T06:40:23.230 回答
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

于 2012-05-18T06:49:37.230 回答
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]];
于 2012-05-18T07:05:55.413 回答
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;
}
于 2012-05-20T12:29:34.143 回答