ios - 如何在 watch 应用程序中缓存图像?

标签 ios objective-c watchkit

我正在使用以下代码缓存图像-来自Apple Doc

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{

            NSString *url_string = [NSString stringWithFormat:@"%@",[rowData valueForKey:@"thumbnail_media_id"]];
            NSURL *url = [NSURL URLWithString:url_string];

            NSData * imageData = [NSData dataWithContentsOfURL:url];
            dispatch_async(dispatch_get_main_queue(), ^{

                UIImage *image = [UIImage imageWithData:imageData];
                // add image in cache
                [[WKInterfaceDevice currentDevice] addCachedImage:image name:url_string];

                [elementRow.rowImage setImage:image];
            });
        });

当从缓存中获取图像时,检查图像名称中是否包含
[[WKInterfaceDevice currentDevice] cachedImages];

然后使用方法获取图像-
[elementRow.rowImage setImageNamed:url_string];

但是无法获取缓存的图像,有什么帮助吗?

最佳答案

您可以使用NSCache缓存图像,但是您可能不应该使用dataWithContentsOfURL。 NSURLSession具有内置的有用的缓存。

http://nshipster.com/nsurlcache/

关于ios - 如何在 watch 应用程序中缓存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29544841/

相关文章:

objective-c - 需要有关在 quartz-2d 中从中心增大和缩小圆的帮助

ios - 如何从磁盘上的视频文件中获取 NSData

objective-c - 通过触摸检测 View 内的更近点

ios - HealthKit Watch 应用程序未记录卡路里

ios - 如何检查 iPhone 和 Apple Watch 是否连接

ios - 无法使用 WatchConnectivity (swift) 发送自定义对象

ios - 在iOS中禁用了Twilio扬声器音频/视频通话

objective-c - 用于 objective-c 的 Twitter SDK

objective-c - 如何在 Objective-c 中将数组声明为常量?

objective-c - 如何在我从该控件之一的子类中放置控件的 View 中使用方法