简体   繁体   中英

How to cache images in watch app?

I am caching image with following code - From 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];
            });
        });

And when getting the image from cache checking if image name contains in

[[WKInterfaceDevice currentDevice] cachedImages];

then get the image with method -

[elementRow.rowImage setImageNamed:url_string];

But not able to get the cached image, any help ?

You could cache image using NSCache but you probably should not be using dataWithContentsOfURL. NSURLSession has helpful caching built right in.

http://nshipster.com/nsurlcache/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM