在iPhone上高效地存储和检索图像
我有一个应用程序,它不加载几个图像并将它们存储在手机上。总共可能需要大约20张图片。我需要能够检索任何这些图像在任意取决于什么屏幕上的用户。这些图像将无限期地存储,所以我不想使用临时目录 目前,我有一个名为Images的类使用这些方法在iPhone上高效地存储和检索图像,iphone,objective-c,Iphone,Objective C,我有一个应用程序,它不加载几个图像并将它们存储在手机上。总共可能需要大约20张图片。我需要能够检索任何这些图像在任意取决于什么屏幕上的用户。这些图像将无限期地存储,所以我不想使用临时目录 目前,我有一个名为Images的类使用这些方法 - (void) cacheImage: (NSString *) ImageURLString : (NSString *)imageName { NSURL *ImageURL = [NSURL URLWithString: ImageURLStrin
- (void) cacheImage: (NSString *) ImageURLString : (NSString *)imageName
{
NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
// Generate a unique path to a resource representing the image you want
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: imageName];
// Check for file existence
if(![[NSFileManager defaultManager] fileExistsAtPath: docFile])
{
// The file doesn't exist, we should get a copy of it
// Fetch image
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
UIImage *image = [[UIImage alloc] initWithData: data];
// Is it PNG or JPG/JPEG?
// Running the image representation function writes the data from the image to a file
if([ImageURLString rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound)
{
[UIImagePNGRepresentation(image) writeToFile: docFile atomically: YES];
}
else if([ImageURLString rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound ||
[ImageURLString rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound)
{
[UIImageJPEGRepresentation(image, 100) writeToFile: docFile atomically: YES];
}
}
}
- (UIImage *) getCachedImage : (NSString *)imageName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* cachedPath = [documentsDirectory stringByAppendingPathComponent:imageName];
UIImage *image;
// Check for a cached version
if([[NSFileManager defaultManager] fileExistsAtPath: cachedPath])
{
image = [UIImage imageWithContentsOfFile: cachedPath]; // this is the cached image
}
else
{
NSLog(@"Error getting image %@", imageName);
}
return image;
}
-(void)getImages
{
//example
NSString *image1URL = @"http://test/image1.png";
NSString *image2URL = @"http://test/image2.png";
NSString *image3URL = @"http://test/image3.png";
[self cacheImage:sLogo: @"Image1"];
[self cacheImage:sBlankNav: @"Image2"];
[self cacheImage:buttonLarge :@"Image3"];
}
-(void) storeImages
{
image1 = [self getCachedImage:@"Image1"];
image2 = [self getCachedImage:@"Image2"];
image3 = [self getCachedImage:@"Image3"];
}
所以我使用这样的代码
Images *cache = [[Images alloc]init];
[cache storeImages];
get images方法在应用程序首次开始获取图像时被调用一次,之后不会再次调用,除非服务器上的图像已更新,并且我需要检索更新的图像
代码可以工作,但问题是当我导航到使用它的屏幕时,在屏幕加载图像之前会有一个非常小的延迟
我的应用程序是一个选项卡式应用程序,所以它从选项卡1开始,我单击实现代码的选项卡2,第一次加载时会有一个轻微的暂停。它不会持续很长时间,但它是明显的,是非常恼人的。之后就可以了,因为它已经加载了。但是,使用导航控制器,每次从第一个VC移动到第二个VC时,都会再次调用该方法,因此每次导航时都会出现延迟
图像不是很大,最大的一个是68kb,其他的比那个小很多。目前我只测试了5张图片。是否有一种更有效的方法来存储和检索图像,或者我的代码是否有问题?为了使我的应用程序保持流畅,而不是急促或笨拙,我需要能够在没有任何明显延迟的情况下检索这些图像
提前谢谢 您有延迟,因为您正在同步下载数据
// NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
尝试一些智能库,如SDWebImage:
它允许您异步下载图像,同时仍然可以显示本地图像(代理图像)。顺便说一下,您仍然可以免费获得缓存映像。所以,即使你在本地,你仍然可以捕获以前下载的图像
A必须具有您有两个选项可以在后台线程上执行图像加载工作-使用或。GCD可被视为两种清洁剂中的清洁剂:
dispatch_queue_t q = dispatch_get_global_queue(0, 0);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(q, ^{
//load images here
dispatch_async(main, ^{
// show on main thread here
});
});
一个有趣的图书馆将不得不检查它。然而,问题不在于下载,因为我只下载了一次,而在于检索手机上存储的图像。我注意到库也进行缓存,它是否允许您在缓存这些图像后快速检索它们?问题是,在检索图像时不会显示图像,然后图像会突然出现。这是一个值得注意的问题,在导航控制器中,这并不像在屏幕上导航一秒钟那么糟糕,但是对于选项卡屏幕,它们会立即出现,因此您可以看到图像会突然出现。你不认为这周围有什么,是吗?实际上这个方法非常有效。我所做的就是在AppDelegate中创建一个对image对象的强引用,调用该方法中的store images,然后每当我需要使用这些图像时,只要调用在AppDelegate中创建的image对象,因为这些图像已经存储。不再有延迟问题,也不再有创建映像的延迟。干杯