1

有没有办法将从网络下载的图像插入设备缓存?在我的应用程序中,有一些包含图像的视图。在每个 tableView 有 25-30 图像。问题是图像在滚动时加载,结果是滚动图像需要 2-3 秒才能重新加载...看起来不太好:) 目前,我在每个单元格都使用 NSURLRequest。

NSURL* url = [NSURL URLWithString:article.articleImage];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) 
{
     if (!error)
     {
         UIImage* image = [[UIImage alloc] initWithData:data];
                                           MainImg.image = image;
                                           article.articleImg=image;
     }

}];

我尝试使用多线程,但应用程序的启动时间太长。想法?

4

4 回答 4

0

用这个:

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
    NSString *savedImagePath = [NSString stringWithFormat:@"%@/%@",[paths objectAtIndex:0],[article.articleImage lastPathComponent]];
    UIImage *image = [UIImage imageNamed:savedImagePath];
    if(!image)
    {
       NSURL* url = [NSURL URLWithString:article.articleImage];
       NSURLRequest* request = [NSURLRequest requestWithURL:url];
       [NSURLConnection sendAsynchronousRequest:request
       queue:[NSOperationQueue mainQueue]
       completionHandler:^(NSURLResponse * response,
       NSData * data,
       NSError * error) 
       {
         if (!error)
         {
            [data writeToFile:savedImagePath atomically:NO]; 
             UIImage* image = [UIImage imageNamed:savedImagePath];
             MainImg.image = image;
             article.articleImg=image;
         }

    }];
  }
else
{
   MainImg.image = image;
   article.articleImg=image;
}
于 2013-06-13T13:16:13.187 回答
0

您可以为此目的使用 NSURLCache。昨天我和这个一起工作,我上课:

@interface MyURLCache : NSURLCache
@property (nonatomic, assign) NSTimeInterval timeAvaiable;
@end

@implementation MyURLCache

- (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path
{
    self = [super initWithMemoryCapacity:memoryCapacity diskCapacity:diskCapacity diskPath:path];
    if(self){
        _timeAvaiable = 5*60;//5 мин
    }
    return self;
}
- (id)init
{
    self = [super init];
    if(self){
        _timeAvaiable = 5*60;
    }
    return self;
}

+ (NSCachedURLResponse*)addInfoDataToCachedResponce:(NSCachedURLResponse*)cachedResponse
{
    NSMutableDictionary* newUserInfo = [@{@"LastUpdate" : @([[NSDate date] timeIntervalSince1970])} mutableCopy];
    if([cachedResponse userInfo])
        newUserInfo[@"UserInfo"] = [cachedResponse userInfo];

    return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response]
                                                    data:[cachedResponse data]
                                                userInfo:newUserInfo
                                           storagePolicy:[cachedResponse storagePolicy]];
}

+ (NSCachedURLResponse*)removeInfoDataFromCachedResponce:(NSCachedURLResponse*)cachedResponse
{
    return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response]
                                                    data:[cachedResponse data]
                                                userInfo:[cachedResponse userInfo][@"UserInfo"]
                                           storagePolicy:[cachedResponse storagePolicy]];
}

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
    NSCachedURLResponse* result = [super cachedResponseForRequest:request];
    NSDate* lastUpdate = [NSDate dateWithTimeIntervalSince1970:[result.userInfo[@"LastUpdate"] doubleValue]];
    BOOL expired = fabs([lastUpdate timeIntervalSinceNow]) > _timeAvaiable;
    return expired? nil : [[self class] removeInfoDataFromCachedResponce:result];
}

- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request
{
    [super storeCachedResponse:[[self class] addInfoDataToCachedResponce:cachedResponse] forRequest:request];
}

@end

并且在应用加载时启用缓存:

MyURLCache *URLCache = [[MyURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                     diskCapacity:20 * 1024 * 1024
                                                         diskPath:[SDURLCache defaultCachePath]];
[NSURLCache setSharedURLCache:URLCache];

对于每个请求都需要将 cachePolicy 设置为 NSURLRequestReturnCacheDataElseLoad。

或者只是使用 AFNetworking :)

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
              placeholderImage:(UIImage *)placeholderImage 
                       success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                       failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
于 2013-06-13T12:20:40.027 回答
0

你好你需要 UIImageView+AFNetworking.h 类别下载和使用AFNetworking你排序

使用它的 - (void)setImageWithURL:(NSURL *)url 方法就像魅力一样

于 2013-06-13T12:12:32.013 回答
0

在我所有的应用程序中,我都在使用这个框架。使用非常简单

https://github.com/rs/SDWebImage

代码示例

   [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]

];

于 2013-06-13T12:50:30.150 回答