IOS UI 线程与非 UI 线程代码收集
直接分出到子线程中
[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];
});
});
-------------------------
//新建一个队列
dispatch_queue_t concurrentQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//执行concurrentQueue队列
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(), ^{
/* 在主线程里面显示图片*/
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(@"从相册中取最后一张图片失败. 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));
}
}