0

嗨,我有一堆从网上下载的照片,我想将它们存储在一个目录中,这样我就不必重新加载已经下载的照片,我无法理解如何执行此操作并存储照片并重新加载它们目录等。这就是我到目前为止所拥有的。有人可以解释所涉及的步骤以及如何做吗?谢谢

-(void) savePhotoInCache: (NSData*) photoToSave{
   //I dont know if u need bundle ID?
   // NSString * bundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager *fm = [[NSFileManager alloc] init];
NSArray * directoryPaths = [fm URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
NSLog(@"%@", directoryPaths);
NSURL* dirPath = nil;    
 //Does this create a file in my cache Directory to store my photos?
dirPath = [[directoryPaths objectAtIndex:0] URLByAppendingPathComponent:[NSString stringWithFormat:@"photos.jpg"]];
NSError* theError = nil;
[fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theError];
// Saves the photo to the file?
[photoToSave writeToURL: dirPath atomically:NO];

NSLog(@"%@", dirPath);
//I get a deprecated warning, new version needs encoding, but I did not specify encoding in writeToURL so what do I use?
NSString * contents = [NSString stringWithContentsOfURL:dirPath ];

//After this how to I access my files and check what the contents of the file are? also, how do I limit the amount of information it stores? thanks

}
4

2 回答 2

0

您不需要自己处理所有事情。我宁愿使用EGO Image Loader,它可以为您完成所有工作。它将图像保存(缓存)在应用程序包目录中,并在您需要时重用它们。就学习类而言,它使用图像 url 作为主键(从缓存中检索图像)。它非常快且异步。

只需将其添加到您的项目中并像这样调用它:(这是ARC,所以我不使用-release)

UIImageView *imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];
imageView.frame = CGRectMake(0.0f, 44.0f, 320.0f,54.0f);
imageView.imageURL = [NSURL URLWithString:@"www.example.com/1.jpg"] ;
[self.view addSubview:imageView];

我希望它有用。

于 2012-08-16T06:53:26.323 回答
0

检查这个如何获取保存的内容,如下所示:

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil];

现在过滤器包含这样的内容:

NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"];
NSPredicate *fltr1 = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:fltr];
NSArray *onlyPNGs = [dirContents filteredArrayUsingPredicate:fltr1];

获取所有图像内容

for(NSString *fileName in dirContents)
{
    NSString *imgPath = [dirPath stringByAppendingFormat:fileName];
    UIImage *img = [UIImage imageWithContentsOfFile:imgPath];
    //You have image use it where u want
}

如果您有很多图像,则使用延迟加载图像

于 2012-08-16T07:17:08.280 回答