[IOS animation]-calayer image io One

Source: Internet
Author: User

Image io

incubation period worth thinking about -Kevin Passat

In the 13th chapter, "Efficient drawing," we studied the performance issues associated with core graphics drawings and how to fix them. Closely related to drawing performance is image performance. In this chapter, we'll look at how to optimize loading and displaying images from a flash drive or network.

Loading and lurking

The actual time that the drawing is consumed is not usually a factor that affects performance. The picture consumes a large portion of memory and is unlikely to keep the images that need to be displayed in memory, so you need to load and unload the pictures periodically while the app is running.

The speed at which the picture file is loaded is affected by both CPU and IO (input/output). Flash memory in iOS devices is already much faster than traditional hard drives, but is still about 200 times times slower than RAM, which requires careful management of loading to avoid delays.

Whenever possible, try to load a picture when it is not noticeable in the program's life cycle, such as booting, or during screen switching. The maximum delay between pressing the button and the button response event is approximately 200ms, which is much smaller than the 16ms of the animation for each frame switch. You can load the image when the program first launches, but if the program fails to start within 20 seconds, the iOS detection timer will terminate your app (and the user will complain if the boot is greater than 2, 3 seconds).

Sometimes it's not wise to load all things ahead of time. For example, a picture carousel with thousands of pictures: users want to be able to quickly flip a picture smoothly, so it is impossible to preload all of the images in advance, which consumes too much time and memory.

Sometimes pictures also need to be downloaded from a remote network connection, which consumes more time than loading from disk, and may even fail to load due to a connection problem (after a few seconds of trying). You cannot load the network in the main thread to cause waiting, so you need a background thread.

Line loads load

In the 12th chapter, "Performance Tuning" Our contact list example, the images are very small, so they can be loaded synchronously on the main thread. But for larger graphs, this is not a good thing to do, because loading can take a long time, resulting in a non-smooth slide. Slide animations are updated in the main thread's run loop, so there are more CPU-related performance issues running in the rendering service process.

Listing 14.1 shows a through  uicollectionview   Implements the underlying picture transmitter. Picture in the main thread  -collectionview:cellforitematindexpath: Synchronous loading in the   method (see Figure 14.1).

Listing 14.1 using UICollectionView the implemented picture transporter


#import   "ViewController.h" @interface  viewcontroller ()  <uicollectionviewdatasource>@ property  (nonatomic, copy)  NSArray *imagePaths; @property   (nonatomic, weak)   iboutlet uicollectionview *collectionview;@[email protected] viewcontroller-  (void) viewdidload{    //set up data    self.imagepaths =     [[nsbundle mainbundle] pathsforresourcesoftype:@ "PNG"  inDirectory:@ " Vacation photos "];    //register cell class    [ self.collectionview registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:@ " Cell "];} -  (Nsinteger) CollectionView: (uicollectionview *) Collectionview numberofitemsinsection: ( Nsinteger) Section{    return [self.imagepaths count];} -  (uicollectionviewcell *) CollectionView: (uicollectionview *) collectionview                   cellforitematindexpath: (nsindexpath *) indexpath{    // dequeue cell    uicollectionviewcell *cell = [collectionview  dequeuereusablecellwithreuseidentifier:@ "Cell"  forindexpath:indexpath];    //add  image view    const NSInteger imageTag = 99;     UIImageView *imageView =  (uiimageview *) [cell viewwithtag:imagetag];     if  (!imageview)  {        imageview  = [[UIImageView alloc] initWithFrame: cell.contentView.bounds];         imageView.tag = imageTag;         [cell.contentview addsubview:Imageview];    }    //set image    nsstring  *imagepath = self.imagepaths[indexpath.row];    imageview.image =  [uiimage imagewithcontentsoffile:imagepath];    return cell;} @end



Figure 14.1 The picture transmitter in operation

The image size of the transmitter is 800x600 pixels of PNG, for IPhone5, 1/60 seconds to load about 700KB of pictures. When the transmitter is scrolling, the picture is loaded in real time, and then the (expected) snap is taken. The Time analysis tool (Figure 14.2) shows that a lot of time is consumed in the  uiimage    +imagewithcontentsoffile: The   method. Obviously, picture loading creates a bottleneck.


[IOS animation]-calayer image io One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.