NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *loadingImageDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"STH.app"];
downloadFilepath = [loadingImageDirectory stringByAppendingPathComponent:@"Default.png"];
NSMutableDictionary *paramDict = [[NSMutableDictionary alloc] init];
[paramDict setValue:@"1" forKey:@"clientId"];
[paramDict setValue:@"0" forKey:@"type"];
STHCommandResult *result = [[STHManager instance] doCommand:SCGetCommonParam :paramDict];
if (result.errorCode == ECOK) {
NSString* imageUrlFilePath = [documentsDirectory stringByAppendingPathComponent:@"imageUrl.plist"];
NSURL *loadingImageUrl = [NSURL URLWithString:[(SOSetting *)[[STHManager instance].sthInfo.commonParamSet objectForKey:@"0"] value]];
if([[NSFileManager defaultManager]fileExistsAtPath:imageUrlFilePath]) //如果记录图片地址文档存在
{
NSMutableString* imageUrlString = [[NSMutableString alloc]initWithContentsOfFile:imageUrlFilePath encoding:NSUnicodeStringEncoding error:nil];
if(![[loadingImageUrl absoluteString] isEqualToString:imageUrlString])
{
[imageUrlString setString:[loadingImageUrl absoluteString]];
[imageUrlString writeToFile:imageUrlFilePath atomically:YES encoding:NSUnicodeStringEncoding error:nil];
// 异步加载
UIImage *cachedImage = [ [STHManager instance].webImageManager imageWithURL:loadingImageUrl];
if (cachedImage) {
// 如果Cache命中,则直接利用缓存的图片进行有关操作
NSData *lodingImage = UIImagePNGRepresentation(cachedImage);
[lodingImage writeToFile:downloadFilepath atomically:YES];
} else {
// 如果Cache没有命中,则去下载指定网络位置的图片,并且给出一个委托方法
// Start an async download
[[STHManager instance].webImageManager downloadWithURL:loadingImageUrl delegate:self];
}
}
}
else { // 如果不存在,则创建文档
NSMutableString* imageUrlString = [[NSMutableString alloc]initWithString:loadingImageUrl.absoluteString];
[imageUrlString writeToFile:imageUrlFilePath atomically:YES encoding:NSUnicodeStringEncoding error:nil];
// 异步加载
UIImage *cachedImage = [ [STHManager instance].webImageManager imageWithURL:loadingImageUrl];
if (cachedImage) {
// 如果Cache命中,则直接利用缓存的图片进行有关操作
} else {
// 如果Cache没有命中,则去下载指定网络位置的图片,并且给出一个委托方法
// Start an async download
[[STHManager instance].webImageManager downloadWithURL:loadingImageUrl delegate:self];
}
}
}
//注:此文章仅供思路,如有朋友需求此功能,还需亲自尝试