Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use GCD to load and transfer pictures

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use GCD to load and transmit pictures", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use GCD to load and transfer pictures" this article.

Use GCD to load and transfer pictures

If you have a lot of pictures to show, it's best not to load them all in advance, but to destroy them as soon as you move them off the screen. By selectively caching, you can avoid repeatedly loading images when scrolling back and forth.

Caching is really simple: it stores the results of expensive calculations (or files loaded from flash or network) in memory for later use, which makes it easy to access. The problem is that caching is essentially a tradeoff-memory is consumed to improve performance, but because memory is a very valuable resource, you can't cache everything.

It is not always obvious when to cache what (for how long). Fortunately, in most cases, iOS caches images for us.

[UIImageimageNamed:] method

We mentioned earlier that the advantage of using [UIImageimageNamed:] to load a picture is that it can be decompressed immediately without having to wait until it is drawn. But the [UIImageimageNamed:] method has another significant benefit: it automatically caches the uncompressed image in memory, even if you don't keep any references to it yourself.

Using [UIImageimageNamed:] to load images is the easiest and most efficient way for iOS apps to load major images (such as icons, buttons, and background images). The same mechanism applies to images referenced in nib files, so you use it implicitly a lot of times.

But [UIImageimageNamed:] does not apply in any case. It is optimized for the user interface, but not for all types of images that the application needs to display. Sometimes you still have to implement your own caching mechanism for the following reasons:

The [UIImageimageNamed:] method is only suitable for images in the application resource bundle directory, but many images for most applications are obtained from the network or the user's camera, so [UIImageimageNamed:] is not available.

[UIImageimageNamed:] the cache is used to store the pictures (buttons, backgrounds, etc.) of the application interface. If this cache is also used for large images like photos, then the iOS system is likely to remove these images to save memory. Then performance degrades when switching pages because these images need to be reloaded. Using a separate caching mechanism for the image of the transmitter can decouple it from the life cycle of the application image.

Caching mechanism of [UIImageimageNamed:]

[UIImageimageNamed:] the caching mechanism is not public, so you can't control it very well. For example, you can't detect whether an image is cached before it is loaded, you can't set the cache size, and you can't remove it from the cache when it's useless.

It is very difficult to build a so-called caching system. Phil Carlton once said, "there are only two difficult things in computer science: caching and naming."

The above is all the contents of the article "how to use GCD to load and transfer pictures". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report