如何在 Objective-C 中缓存图像

How to cache images in Objective-C

提问人:Didami 提问时间:2/21/2021 更新时间:2/21/2021 访问量:838

问:

我想创建一个从 URL 缓存图像的方法,我在 Swift 中获得了代码,因为我以前使用过它,我如何在 Objective-C 中做类似的事情:

import UIKit

let imageCache: NSCache = NSCache<AnyObject, AnyObject>()

extension UIImageView {
    
    func loadImageUsingCacheWithUrlString(urlString: String) {
        
        self.image = nil
        
        if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
            self.image = cachedImage
            return
        }
        
        let url = URL(string: urlString)
        if let data = try? Data(contentsOf: url!) {
            
            DispatchQueue.main.async(execute: {
                
                if let downloadedImage = UIImage(data: data) {
                    imageCache.setObject(downloadedImage, forKey: urlString as AnyObject)
                    
                    self.image = downloadedImage
                }
            })
        }
    }
    
}
Objective-C URL 缓存 UIIomeView NSCache

评论

0赞 CouchDeveloper 2/23/2021
使用此作业的第三方库。有许多高质量的开源项目。

答:

0赞 Didami 2/21/2021 #1

经过反复试验,这奏效了:

#import "UIImageView+Cache.h"

@implementation UIImageView (Cache)

NSCache* imageCache;

- (void)loadImageUsingCacheWithUrlString:(NSString*)urlString {
    
    imageCache = [[NSCache alloc] init];
    
    self.image = nil;
    
    UIImage *cachedImage = [imageCache objectForKey:(id)urlString];
    
    if (cachedImage != nil) {
        self.image = cachedImage;
        return;
    }
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    if (data != nil) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            UIImage *downloadedImage = [UIImage imageWithData:data];
            
            if (downloadedImage != nil) {
                [imageCache setObject:downloadedImage forKey:urlString];
                self.image = downloadedImage;
            }
        });
    }
}

@end
2赞 Rob 2/21/2021 #2

在转换此内容之前,可以考虑重构以使其异步:

  1. 永远不应该用于网络请求,因为 (a) 它是同步的,会阻止调用方(这是一个可怕的用户体验,但在退化的情况下,也可能导致看门狗进程杀死你的应用程序);(b) 如果有问题,则没有诊断信息;及 (c) 不可取消。Data(contentsOf:)

  2. 不应在完成后更新属性,而应考虑完成处理程序模式,以便调用方知道请求何时完成并处理图像。此模式避免了争用条件,并允许你同时发出图像请求。image

  3. 使用此异步模式时,将在后台队列上运行其完成处理程序。应将映像的处理和缓存的更新保留在此后台队列上。只有完成处理程序应调度回主队列。URLSession

  4. 我从你的回答中推断,你的意图是在扩展中使用这段代码。你真的应该把这段代码放在一个单独的对象中(我创建了一个单例),这样这个缓存就不仅适用于图像视图,而且可用于任何可能需要图像的地方。例如,您可以对 .如果此代码隐藏在UIImageViewImageManagerUIImageView

因此,也许是这样的:

final class ImageManager {
    static let shared = ImageManager()

    enum ImageFetchError: Error {
        case invalidURL
        case networkError(Data?, URLResponse?)
    }

    private let imageCache = NSCache<NSString, UIImage>()

    private init() { }

    @discardableResult
    func fetchImage(urlString: String, completion: @escaping (Result<UIImage, Error>) -> Void) -> URLSessionTask? {
        if let cachedImage = imageCache.object(forKey: urlString as NSString) {
            completion(.success(cachedImage))
            return nil
        }

        guard let url = URL(string: urlString) else {
            completion(.failure(ImageFetchError.invalidURL))
            return nil
        }

        let task = URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                error == nil,
                let responseData = data,
                let httpUrlResponse = response as? HTTPURLResponse,
                200 ..< 300 ~= httpUrlResponse.statusCode,
                let image = UIImage(data: responseData)
            else {
                DispatchQueue.main.async {
                    completion(.failure(error ?? ImageFetchError.networkError(data, response)))
                }
                return
            }

            self.imageCache.setObject(image, forKey: urlString as NSString)
            DispatchQueue.main.async {
                completion(.success(image))
            }
        }

        task.resume()

        return task
    }

}

你可以这样称呼它:

ImageManager.shared.fetchImage(urlString: someUrl) { result in
    switch result {
    case .failure(let error): print(error)
    case .success(let image): // do something with image
    }
}

// but do not try to use `image` here, as it has not been fetched yet

例如,如果要在扩展中使用它,则可以保存 ,以便在上一个图像完成之前请求另一个图像时可以取消它。(例如,如果在表视图中使用它并且用户滚动速度非常快,则这是一个非常常见的方案。您不希望在大量网络请求中积压。我们可以UIImageViewURLSessionTask

extension UIImageView {
    private static var taskKey = 0
    private static var urlKey = 0

    private var currentTask: URLSessionTask? {
        get { objc_getAssociatedObject(self, &Self.taskKey) as? URLSessionTask }
        set { objc_setAssociatedObject(self, &Self.taskKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
    }

    private var currentURLString: String? {
        get { objc_getAssociatedObject(self, &Self.urlKey) as? String }
        set { objc_setAssociatedObject(self, &Self.urlKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
    }

    func setImage(with urlString: String) {
        if let oldTask = currentTask {
            currentTask = nil
            oldTask.cancel()
        }

        image = nil

        currentURLString = urlString

        let task = ImageManager.shared.fetchImage(urlString: urlString) { result in
            // only reset if the current value is for this url

            if urlString == self.currentURLString {
                self.currentTask = nil
                self.currentURLString = nil
            }

            // now use the image

            if case .success(let image) = result {
                self.image = image
            }
        }

        currentTask = task
    }
}

在这个扩展中,你可能会做很多其他的事情(例如占位符图像等),但是通过将扩展与网络层分离,可以将这些不同的任务保留在各自的类中(本着单一责任原则的精神)。UIImageViewUIImageView


好了,说到这里,让我们看看 Objective-C 的演绎版。例如,您可以创建一个单例:ImageManager

//  ImageManager.h

@import UIKit;

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, ImageManagerError) {
    ImageManagerErrorInvalidURL,
    ImageManagerErrorNetworkError,
    ImageManagerErrorNotValidImage
};

@interface ImageManager : NSObject

// if you make this singleton, mark normal instantiation methods as unavailable ...

+ (instancetype)alloc __attribute__((unavailable("alloc not available, call sharedImageManager instead")));
- (instancetype)init __attribute__((unavailable("init not available, call sharedImageManager instead")));
+ (instancetype)new __attribute__((unavailable("new not available, call sharedImageManager instead")));
- (instancetype)copy __attribute__((unavailable("copy not available, call sharedImageManager instead")));

// ... and expose singleton access point

@property (class, nonnull, readonly, strong) ImageManager *sharedImageManager;

// provide fetch method

- (NSURLSessionTask * _Nullable)fetchImageWithURLString:(NSString *)urlString completion:(void (^)(UIImage * _Nullable image, NSError * _Nullable error))completion;

@end

NS_ASSUME_NONNULL_END

然后实现这个单例:

//  ImageManager.m

#import "ImageManager.h"

@interface ImageManager()
@property (nonatomic, strong) NSCache<NSString *, UIImage *> *imageCache;
@end

@implementation ImageManager

+ (instancetype)sharedImageManager {
    static dispatch_once_t onceToken;
    static ImageManager *shared;
    dispatch_once(&onceToken, ^{
        shared = [[self alloc] initPrivate];
    });
    return shared;
}

- (instancetype)initPrivate
{
    self = [super init];
    if (self) {
        _imageCache = [[NSCache alloc] init];
    }
    return self;
}

- (NSURLSessionTask *)fetchImageWithURLString:(NSString *)urlString completion:(void (^)(UIImage *image, NSError *error))completion {
    UIImage *cachedImage = [self.imageCache objectForKey:urlString];
    if (cachedImage) {
        completion(cachedImage, nil);
        return nil;
    }

    NSURL *url = [NSURL URLWithString:urlString];
    if (!url) {
        NSError *error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:ImageManagerErrorInvalidURL userInfo:nil];
        completion(nil, error);
        return nil;
    }

    NSURLSessionTask *task = [NSURLSession.sharedSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, error);
            });
            return;
        }

        if (!data) {
            NSError *error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:ImageManagerErrorNetworkError userInfo:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, error);
            });
        }

        UIImage *image = [UIImage imageWithData:data];
        if (!image) {
            NSDictionary *userInfo = @{
                @"data": data,
                @"response": response ? response : [NSNull null]
            };
            NSError *error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:ImageManagerErrorNotValidImage userInfo:userInfo];
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, error);
            });
        }

        [self.imageCache setObject:image forKey:urlString];
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(image, nil);
        });
    }];

    [task resume];

    return task;
}

@end

你可以这样称呼它:

[[ImageManager sharedImageManager] fetchImageWithURLString:urlString completion:^(UIImage * _Nullable image, NSError * _Nullable error) {
    if (error) {
        NSLog(@"%@", error);
        return;
    }

    // do something with `image` here ...
}];

// but not here, because the above runs asynchronously

同样,您可以在扩展中使用它:UIImageView

#import <objc/runtime.h>

@implementation UIImageView (Cache)

- (void)setImage:(NSString *)urlString
{
    NSURLSessionTask *oldTask = objc_getAssociatedObject(self, &taskKey);
    if (oldTask) {
        objc_setAssociatedObject(self, &taskKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [oldTask cancel];
    }

    image = nil

    objc_setAssociatedObject(self, &urlKey, urlString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    NSURLSessionTask *task = [[ImageManager sharedImageManager] fetchImageWithURLString:urlString completion:^(UIImage * _Nullable image, NSError * _Nullable error) {
        NSString *currentURL = objc_getAssociatedObject(self, &urlKey);
        if ([currentURL isEqualToString:urlString]) {
            objc_setAssociatedObject(self, &urlKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
            objc_setAssociatedObject(self, &taskKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }

        if (image) {
            self.image = image;
        }
    }];

    objc_setAssociatedObject(self, &taskKey, task, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end