首页 > 其他 > 详细

自己写的获取网络图片,不放内存里,包括文件的删除,创建

时间:2015-11-07 02:16:24      阅读:326      评论:0      收藏:0      [点我收藏+]

//

//? UIImageView+CacheURL.m

//? Vodka

//

//? Created by xiaoming on 15/11/5.

//? Copyright ? 2015 Beijing Beast Technology Co.,Ltd. All rights reserved.

//

?

#import "UIImageView+CacheURL.h"

#import "CodeFragments.h"

?

@implementation UIImageView (CacheURL)

?

#pragma mark - 传人urlString 和默认图。获取网络图片。----不放到缓存里面

-(void)setURLString:(NSString *)urlString placeholderImage:(UIImage *)image{

? ? [selfsetURLString:urlString placeholderImage:image relativePath:nil];

}

?

#pragma mark - 传人urlString 和默认图。获取网络图片。----不放到缓存里面 relativePath nil @"" 跟上面的方法一样,都是 tempCache 路径下。

-(void)setURLString:(NSString *)urlString placeholderImage:(UIImage *)defaultImage relativePath:(NSString *)relativePath{

? ? if (relativePath == nil || [relativePath isEqualToString:@""]) {

? ? ? ? relativePath = @"tempCache";

? ? }

? ? self.image = defaultImage;

? ? if ([self isEmptyString:urlString]) {

? ? ? ? return;

? ? }

? ? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

? ? ? ? UIImage *resultImage = nil;///从网上获取成功后的image

? ? ? ? //从缓存中读取。

? ? ? ? resultImage = [UIImage imageWithContentsOfFile:[self absolutePath:relativePath urlString:urlString systemPath:nil]];

? ? ? ? if (resultImage) {

? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ? self.image = resultImage;

? ? ? ? ? ? });

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? ///没有缓存的情况下。

? ? ? ? NSURL *url = [NSURL URLWithString:urlString];

? ? ? ? NSData *resultData = [NSData dataWithContentsOfURL:url];

? ? ? ? resultImage = [UIImage imageWithData:resultData];

? ? ? ? if (resultImage) {

? ? ? ? ? ? ///缓存data数据。

? ? ? ? ? ? NSString *aFileName = [self absolutePath:relativePath urlString:nil systemPath:nil];

? ? ? ? ? ? NSFileManager *fileManager = [NSFileManager defaultManager];

? ? ? ? ? ? if (![fileManager fileExistsAtPath:aFileName]) {

? ? ? ? ? ? ? ? ////withIntermediateDirectories? YES 表示可以创建多级目录。

? ? ? ? ? ? ? ? if (![fileManager createDirectoryAtPath:aFileName withIntermediateDirectories:YESattributes:nilerror:nil]) {

? ? ? ? ? ? ? ? ? ? NSLog(@"createFile error occurred");

? ? ? ? ? ? ? ? }

?? ? ? ? ? ? ? ?

? ? ? ? ? ? }

? ? ? ? ? ? NSString *resultFileString = [self absolutePath:relativePath urlString:urlString systemPath:nil];

? ? ? ? ? ? if ([resultData writeToFile:resultFileString atomically:YES]) {

? ? ? ? ? ? ? ? NSLog(@"保存成功");

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? NSLog(@"保存失败");

? ? ? ? ? ? }

? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ? self.image = resultImage;

? ? ? ? ? ? });

? ? ? ? }

? ? });

}

?

#pragma mark - 判断是否是空字符串

- (BOOL)isEmptyString:(NSString *)string{

? ? if(string == nil){

? ? ? ? return YES;

? ? }

? ? if([string isKindOfClass:[[NSNull null] class]]){

? ? ? ? return YES;

? ? }

? ? if([string isEqualToString:@""]){

? ? ? ? return YES;

? ? }

? ? if([string isEqualToString:@"<null>"])

? ? ? ? return YES;

? ? if([string isEqualToString:@"(null)"])

? ? ? ? return YES;

? ? returnNO;

}

?

#pragma mark - 从本地文件读取数据。

-(NSString *)absolutePath:(NSString*)relativePath urlString:(NSString *)urlString systemPath:(NSString *)systemPath{

? ? if (relativePath == nil || [relativePath isEqualToString:@""]) {

? ? ? ? relativePath = @"tempCache";

? ? }

? ? NSString *path0 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

? ? NSString *path1 = [path0 stringByAppendingPathComponent:relativePath];

? ? if(urlString && ![urlString isEqualToString:@""]){///创建路径的时候不需要url,读,取的时候,需要url

? ? ? ? NSString *path2 = [path1 stringByAppendingPathComponent:[urlString stringByReplacingOccurrencesOfString:@"/"withString:@"_"]];

? ? ? ? return path2;

? ? }

? ? return path1;

}

?

#pragma mark - 清除本地缓存

-(void)cleanDisk{

? ? NSFileManager *fileManager = [NSFileManagerdefaultManager];

? ? NSString *path0 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

? ? NSString *defaultPath = [NSString stringWithFormat:@"%@/tempCache",path0];

? ? NSLog(@"defaultPaht ==== %@",defaultPath);

? ? [self folderSizeAtPath:defaultPath];

? ? if ([fileManager removeItemAtPath:defaultPath error:nil]) {

? ? ? ? NSLog(@"删除成功");

? ? }else{

? ? ? ? NSLog(@"删除失败");

? ? }

?? ?

? ? [self folderSizeAtPath:defaultPath];

}

?

#pragma mark - 通常用于删除缓存的时,计算缓存大小

//单个文件的大小

- (long long) fileSizeAtPath:(NSString*) filePath{

? ? NSFileManager* manager = [NSFileManagerdefaultManager];

? ? if ([manager fileExistsAtPath:filePath]){

? ? ? ? return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

? ? }

? ? return 0;

}

#pragma mark - 遍历文件夹获得文件夹大小,返回多少M

- (float ) folderSizeAtPath:(NSString*) folderPath{

? ? if (!folderPath || [folderPath isEqualToString:@""]) {

? ? ? ? NSString *path0 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

? ? ? ? folderPath = [NSString stringWithFormat:@"%@/tempCache",path0];

? ? }

? ? NSFileManager* manager = [NSFileManagerdefaultManager];

? ? if (![manager fileExistsAtPath:folderPath])

? ? {

? ? ? ? NSLog(@"大小为0");

? ? ? ? return 0;

? ? }

? ? NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];

? ? NSString* fileName;

? ? long long folderSize = 0;

? ? while ((fileName = [childFilesEnumerator nextObject]) != nil){

? ? ? ? NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

? ? ? ? folderSize += [self fileSizeAtPath:fileAbsolutePath];

? ? }

? ? NSLog(@"文件大小====%f M ",folderSize/(1024.0*1024.0));

? ? return folderSize/(1024.0*1024.0);

}

?

@end

?

自己写的获取网络图片,不放内存里,包括文件的删除,创建

原文:http://zhangmingwei.iteye.com/blog/2254863

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!