iOS Development Summary-Download images and browse (Uicollectionview)

Source: Internet
Author: User

Yesterday according to the Map Library API to do a browse image of the demo, source code in this: https://github.com/beddup/TieTuKuBrowers

Summarized as follows:

1. Use of afnetworking

The demo uses the HTTP API to download data with JSON and image, so Afhttpsessionmanager (based on nsurlsession) is used.

Afnetworking is very convenient, you can convert the JSON directly to Nsarray/nsdictionary, the image data to UIImage, if the correct good Responseserializer (otherwise error).

Responseserializer is used to convert data to the correct type:

-The default Responseserializer is Afjsonresponseserializer (using the JSON parsing tool that comes with iOS), plus:

-Afxmlparserresponseserializer: Parsing xml

-afxmldocumentresponseserializer

-afpropertylistresponseserializer

-afimageresponseserializer

-As well as Afcompoundresponseserializer (combine multiple Afhttpresponseserializer, because the demo data is JSON, also has an image, So the use is this responseserializer)

Simple usage of afhttpsessionmanager (GET),

-Create instance, available-Initwithbaseurl: (Nullable Nsurl *) Url,baseurl that is the previous part of the HTTP address

-Set Responseserializer if necessary

-Make HTTP requests, take get as an example

1-(Nullable Nsurlsessiondatatask *) GET: (NSString *) urlstring2 //urlstring: The HTTP URL resource that makes up the request together with the base URL. 3Parameters: (NullableID) Parameters4 5Success: (Nullablevoid(^) (Nsurlsessiondatatask *task,IDresponseobject)) Success6 //The black,responseobject that are called after the successful capture is the data that has been converted, such as Nsarray, UIImage7Failure: (Nullablevoid(^) (Nsurlsessiondatatask *task, Nserror *error)) Failure

method returns Nsurlsessiondatatask, which sometimes requires this, such as the need to cancel a task later.

2. Uicollectionview & Cell

-Uicolletionview similar to UITableView, DataSource and delegate methods are basically consistent

-Uicolletionview delegate does not have a method to set the section headers and sections footer, but is available

(Uicollectionreusableview *) CollectionView:viewForSupplementaryElementOfKind:atIndexPath: Replace

-Custom cell, it is best to override the Prepareforreuse method, Uicolletionview when Dequeue cell, the method is called before the cell is returned.

The Prepareforreuse method is primarily used to reset the state of the cell, and if the cell's state is not reset, Dequeue returns the cell contents before the cell content dequeue possible

-table view has no corresponding properties and methods in TableHeader and Tablefooter,collectionview, how to set it? (Hint: with Contentinset)

3. Core Data

-When generating nsmanagedobject for entity in Xcode 7, it is more convenient to generate a nsmanagedobject subclass and a coredataproperties category automatically

4. Objective-c and Swift

-Using OBJECTIVE-C in Swift requires a objective-c bridging header, the Objective-c class to use in the. h Import

-Use Swift in Objective-c, requires a objective-c Generated Interface Header, Xcode 7 is automatically generated, and does not appear in bunble, use to import the file to Objective-c File

-in target building setting, Swift complier code generation can see or change the names of these two files.

-This blog post http://blog.csdn.net/mmoaay/article/details/48422309 A good explanation for this.

5. Load network resources at the same time, how to make Uicollectionview smoother

-After a network resource is loaded, the corresponding cell rolls out of the screen at this time, no need to load again, should be canceled save memory

-The retrieved resources should be properly cached.

-If the resource is obtained, it should be determined whether the corresponding cell is visible, if not visible, the cache resource, if visible, use Cellforitematindexpath to get the cell (should not directly use the method of the cell, otherwise the cell does not release, Occupy memory)

-If the cell content is complex, render time-consuming, you can draw the static part asynchronously and then generate an image to add to the cell. You can manage complex tasks with Nsoperationqueue and cancel the task when the corresponding cell rolls out of the screen

-For this, WWDC this video Building Concurrent User interfaces-wwdc2011 speaks very well

6. As described in 5, nsoperationqueue can be used to manage complex time-consuming content and, if necessary, cancel the task

7. The idea of pulling to refresh (loading more) in Uicollectionview

-Set the location of the refresh view in Viewdidlayoutsubviews to the bottom of the Uicollectionview content, not within the Contentsize range

-In Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate

A. Determine if you need to load more according to Contentoffset (in this process you can animate the Refresh view)

B. If you need to load more, set Contentinset to Uiedgeinsetsmake (0, 0, buttom, 0), the value of buttom should > Refresh view height. This way the Uicollectionview will no longer be scroll and the refresh view is displayed, loading the resource

C. After the resource has been loaded, set Contentinset to Uiedgeinsetszero and add contentoffset to make some newly loaded resources visible

iOS Development Summary-Download images and browse (Uicollectionview)

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.