IOS UI thread and non-UI thread code collection

Source: Internet
Author: User

Directly into the child thread

[Nsthread detachnewthreadselector: @selector (setupimageresampling) totarget:self Withobject:nil];

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_low, 0), ^{
[Nsthread sleepfortimeinterval:.5f];

while ([Device Isadjustingfocus] | |
[Device Isadjustingexposure] | |
[Device isadjustingwhitebalance]);

Dispatch_async (Dispatch_get_main_queue (), ^{
//
Remove focus view and focus Subview animated
//
[Camerafocusview stopanimation];
});
});

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_low, 0), ^{
[Nsthread Sleepfortimeinterval:timeinterval];
Dispatch_async (Dispatch_get_main_queue (), ^{
[Self Removeslidefromsuperview:yes
withduration:.5
Originy:[self Finalposition]
Completion:completion];
});
});

-------------------------

Create a new queue
dispatch_queue_t concurrentqueue =dispatch_get_global_queue (dispatch_queue_priority_default, 0);
Execute Concurrentqueue Queue
Dispatch_async (Concurrentqueue, ^{
__block UIImage *image = nil;
Dispatch_sync (Concurrentqueue, ^{
Assethelper.breverse = YES;
[Assethelper getgrouplist:^ (Nsarray *agroups) {
For (Alassetsgroup *group in agroups) {
NSString *groupname = [group Valueforproperty:alassetsgrouppropertyname];
if ([Album_name Isequaltostring:groupname]) {
flag = YES;
Break
}
nindex++;
}

if (flag) {
[Assethelper getphotolistofgroupbyindex:nindex result:^ (Nsarray *aphotos) {
if ([Aphotos count]>0) {
alasset* Assetphotos = [Aphotos objectatindex:0];
Image = [Assethelper getimagefromasset:assetphotos type:asset_photo_thumbnail];
}
}];
}

}];


});
Dispatch_sync (Dispatch_get_main_queue (), ^{
/* display pictures inside the main thread */
if (image = nil) {
CGRect photoframe = CGRectMake (8,6, _albumbutton.frame.size.width-16, _albumbutton.frame.size.height-16);
Uiimageview *imgview = [[Uiimageview alloc] initwithframe:photoframe];
Imgview.image = image;
[_albumbutton Addsubview:imgview];
} else {
NSLog (@ "Failed to take the last picture from the album. Nothing to display. ");
} });


});

-------------------------

void runonmainqueuewithoutdeadlocking (void (^block) (void))
{
if ([Nsthread Ismainthread])
{
Block ();
}
Else
{
Dispatch_sync (Dispatch_get_main_queue (), block);
}
}

void Runsynchronouslyonvideoprocessingqueue (void (^block) (void))
{
dispatch_queue_t videoprocessingqueue = [Gpuimagecontext sharedcontextqueue];
#if! Os_object_use_objc
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-wdeprecated-declarations"
if (dispatch_get_current_queue () = = Videoprocessingqueue)
#pragma clang diagnostic pop
#else
if (dispatch_get_specific ([Gpuimagecontext Contextkey]))
#endif
{
Block ();
}else
{
Dispatch_sync (videoprocessingqueue, block);
}
}

void Runasynchronouslyonvideoprocessingqueue (void (^block) (void))
{
dispatch_queue_t videoprocessingqueue = [Gpuimagecontext sharedcontextqueue];

#if! Os_object_use_objc
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-wdeprecated-declarations"
if (dispatch_get_current_queue () = = Videoprocessingqueue)
#pragma clang diagnostic pop
#else
if (dispatch_get_specific ([Gpuimagecontext Contextkey]))
#endif
{
Block ();
}else
{
Dispatch_async (videoprocessingqueue, block);
}
}

void Runsynchronouslyoncontextqueue (Gpuimagecontext *context, Void (^block) (void))
{
dispatch_queue_t Videoprocessingqueue = [Context Contextqueue];
#if! Os_object_use_objc
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-wdeprecated-declarations"
if (dispatch_get_current_queue () = = Videoprocessingqueue)
#pragma clang diagnostic pop
#else
if (dispatch_get_specific ([Gpuimagecontext Contextkey]))
#endif
{
Block ();
}else
{
Dispatch_sync (videoprocessingqueue, block);
}
}

void Runasynchronouslyoncontextqueue (Gpuimagecontext *context, Void (^block) (void))
{
dispatch_queue_t Videoprocessingqueue = [Context Contextqueue];

#if! Os_object_use_objc
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-wdeprecated-declarations"
if (dispatch_get_current_queue () = = Videoprocessingqueue)
#pragma clang diagnostic pop
#else
if (dispatch_get_specific ([Gpuimagecontext Contextkey]))
#endif
{
Block ();
}else
{
Dispatch_async (videoprocessingqueue, block);
}
}

void Reportavailablememoryforgpuimage (NSString *tag)
{
if (!tag)
Tag = @ "Default";

struct Task_basic_info info;

mach_msg_type_number_t size = sizeof (info);

kern_return_t Kerr = Task_info (Mach_task_self (),

Task_basic_info,

(task_info_t) &info,

&size);
if (Kerr = = kern_success) {
NSLog (@ "%@-Memory used:%u", tag, (unsigned int) info.resident_size); In bytes
} else {
NSLog (@ "%@-Error:%s", tag, mach_error_string (Kerr));
}
}

IOS UI thread and non-UI thread code collection

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.