Multithreading Knowledge points (V)

Source: Internet
Author: User

    1. Download pictures using SD

#import "Uiimageview+webcache.h"

[Self. Hmimageview sd_setimagewithurl:url placeholderimage:nil options:0 progress:^ (Nsinteger receivedSize, NSInteger Expectedsize) {

Receivedsize received file size, total size of expectedsize file} completed:^ (UIImage *image, Nserror *error, Sdimagecachetype CacheType, Nsurl *imageurl) {}];

    1. Use SD to download gif

#import "Uiimage+gif.h"

Mode 1:

Self. Hmimageview.image = [UIImage sd_animatedgifnamed:@ "2"];

Mode 2

Get Picture Path

NSString *path = [[NSBundle mainbundle]pathforresource:@ "2.gif" oftype:nil];

Turn into binary

NSData *data = [NSData Datawithcontentsoffile:path];

Self. Hmimageview.image = [UIImage sd_animatedgifwithdata:data];

    1. Use the system's own cache, using the same way as the dictionary

The first step:

Global cache

@property (Nonatomic,strong) Nscache *cache;

Step Two:

-(Nscache *) cache{

if (_cache = = nil) {

_cache = [[Nscache alloc]init];

Set the number of caches

_cache.countlimit = 5

}

return _cache;

}

Step Three:

Access data

Storing data

[Self.cache setobject:[nsstring stringwithformat:@ "Hello%d", I] forkey:[nsstring stringwithformat:@ "haha%d", I]];

Fourth Step:

Reading data

NSLog (@ "%@", [Self.cache objectforkey:[nsstring stringwithformat:@ "haha%d", I]]);

Method called when the cache is removed

The first step is to abide by the agreement <NSCacheDelegate>

Called when the object is going to be removed

-(void) cache: (Nscache *) cache Willevictobject: (ID) obj

{

NSLog (@ "Willevict%@", obj);

}

4: Message loop: Runloop

There is a message loop inside each thread

Only the main thread's message loop is turned on by default, and the child thread's message loop is not turned on by default

5: The function of the message loop:

1: Can guarantee the application does not exit

2: Ability to handle input events

6. The mode of the input event must match the pattern of the message loop to be able to execute the code Nsdefaultrunloopmode of the input event, Nsrunloopcommonmodes (including the remaining two) Uitrackingrunloopmode

7. Set the mode of the message loop:

[[Nsrunloop Currentrunloop]addtimer:timer Formode:uitrackingrunloopmode];

8. Turn on the sub-thread: [[Nsrunloop Currentrunloop]addtimer:timer Formode:uitrackingrunloopmode];

9. Turn on child threads

[Self Performselector: @selector (Demo) Onthread:thread Withobject:nil Waituntildone:no];

10. The message loop (dead loop) of a child thread is not turned on by default and is manually turned on

[[Nsrunloop Currentrunloop]run];

Or, timing

[[Nsrunloop Currentrunloop]rununtildate:[nsdate datewithtimeintervalsincenow:3.0]];

Multithreading Knowledge points (V)

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.