已解决和更新的代码。
我已经实现了一个将图像缓存到磁盘的图像缓存。它可以将 UIImage 归档NSData
并写入磁盘,[data writeToFile:path options:NSDataWritingAtomic error:&error]
其中路径是 imageURL(从“/”中删除,因此不会被误认为是目录)。除此之外,还有一个字典是键imageURL
,值是时间戳,因此如果字典已满,我可以删除最旧的图像。这是这一切的基本功能。
问题:一切都运行良好一段时间然后(看似随机的场合)我收到此错误:
-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x16eaf810
在以下方法中的这一行 (NSData *registerData = ...) 上:
- (void)persistData:(UIImage *)image forKey:(NSString *)key
{
@synchronized(self) { // looks like this have no effect
NSData *imageData = [NSKeyedArchiver archivedDataWithRootObject:image];
[NSFileManager THSNstoreData:imageData forKey:key];
NSData *registerData = [NSKeyedArchiver archivedDataWithRootObject:_imageCacheRegister]; // debugger stops here
[NSFileManager THSNstoreData:registerData forKey:_dictionaryKey];
}
}
我已经手动检查了字典,并且字典的地址或其任何键/值与错误消息中实例的地址不同。
我在这里真的很困惑......根据我读过的一些东西,__NSCFType
当内存被回收时使用。所以这意味着有些东西会提前发布……或者完全不同的东西。
任何帮助将不胜感激。
更新: 我当然使用 ARC。还有一个依赖注入框架,使我能够将其用作单例。
以下是类中的所有代码:
#import "ImageDiskCache.h"
#import "NSMutableDictionary+Techsson.h"
@interface ImageDiskCache () {
NSString *_dictionaryKey;
NSMutableDictionary *_imageCacheRegister;
dispatch_queue_t _diskQue;
NSInteger _maxCachedImagesCount;
NSInteger _numberOfImagesToRemove;
}
@end
@implementation ImageDiskCache
- (id)init
{
self = [super init];
if (self) {
_maxCachedImagesCount = 50;
_numberOfImagesToRemove = 5;
_dictionaryKey = @"cacheRegistry";
_diskQue = dispatch_queue_create("diskQue", NULL);
dispatch_async(_diskQue, ^{
NSData *data = [NSFileManager THSNdataForKey:_dictionaryKey];
if ( data )
{
_imageCacheRegister = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
else
{
_imageCacheRegister = [[NSMutableDictionary alloc] initWithCapacity:_maxCachedImagesCount];
}
});
}
return self;
}
- (void)readImageForKey:(NSString *)key withCompletionHandler:(UIImageCompletionHandler)completion
{
if ( _imageCacheRegister && key )
{
dispatch_async(_diskQue, ^{
NSString *stripedKey = [self stripedKeyForKey:key];
if ( [_imageCacheRegister safeObjectForKey:stripedKey] )
{
NSData *imageData = [NSFileManager THSNdataForKey:stripedKey];
UIImage *image = imageData ? [NSKeyedUnarchiver unarchiveObjectWithData:imageData] : nil;
dispatch_async(dispatch_get_main_queue(), ^{
completion(image);
});
}
else
{
completion(nil);
}
});
}
else
{
completion(nil);
}
}
- (void)wrightImage:(UIImage *)image forKey:(NSString *)key
{
if ( !image || !key || [key isEqualToString:@""] || !_imageCacheRegister )
{
return;
}
dispatch_async(_diskQue, ^{
NSString *stripedKey = [self stripedKeyForKey:key];
if ( [_imageCacheRegister safeCount] < _maxCachedImagesCount )
{
[_imageCacheRegister safeSetObject:[self timeStampAsData] forKey:stripedKey];
[self persistData:image forKey:stripedKey];
}
else
{
NSArray *keys = [_imageCacheRegister safeKeysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSTimeInterval timeStampObj1 = [self timeStampFromData:obj1];
NSTimeInterval timeStampObj2 = [self timeStampFromData:obj2];
if ( timeStampObj1 > timeStampObj2 )
{
return (NSComparisonResult)NSOrderedAscending;
}
else if ( timeStampObj1 < timeStampObj2 )
{
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSArray *toRemove = [keys subarrayWithRange:NSMakeRange([keys count] - _numberOfImagesToRemove, _numberOfImagesToRemove)];
[_imageCacheRegister safeRemoveObjectsForKeys:toRemove];
for ( NSString *k in toRemove )
{
[NSFileManager THSNdeleteDataForKey:k];
}
[_imageCacheRegister safeSetObject:[self timeStampAsData] forKey:stripedKey];
[self persistData:image forKey:stripedKey];
}
});
}
#pragma - private methods
- (NSString *)stripedKeyForKey:(NSString *)key
{
NSString *stripedKey = [key stringByReplacingOccurrencesOfString:@"/" withString:@""];
return stripedKey;
}
- (NSData *)timeStampAsData
{
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
NSData *timeStampData = [NSData dataWithBytes:&timeStamp length:sizeof(NSTimeInterval)];
return timeStampData;
}
- (NSTimeInterval)timeStampFromData:(NSData *)timeStampData
{
NSTimeInterval timeStamp;
[timeStampData getBytes:&timeStamp length:sizeof(NSTimeInterval)];
return timeStamp;
}
- (void)persistData:(UIImage *)image forKey:(NSString *)key
{
// @synchronized(self) {
NSData *imageData = [NSKeyedArchiver archivedDataWithRootObject:image];
[NSFileManager THSNstoreData:imageData forKey:key];
NSData *registerData = [NSKeyedArchiver archivedDataWithRootObject:_imageCacheRegister];
[NSFileManager THSNstoreData:registerData forKey:_dictionaryKey];
// }
}