2015-04-16 40 views
-1

我想知道如何在全局隊列中截取屏幕截圖?現在我在主隊列中執行它,並且它工作正常,如果我在全局隊列中執行它,事情就會凍結。我使用這個截圖代碼:iOS: what's the fastest, most performant way to make a screenshot programmatically? 我也試過以下代碼,以self.view的快照,但它也不起作用:iOS:我可以在全局隊列上截屏嗎?

+(UIImage *)snapshot_of_view:(UIView*)view { 
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 

什麼想法?

回答

1

不限UI操作必須在主線程進行

+0

我看到這個帖子(http://stackoverflow.com/questions/11528803/is-uigraphicsbeginimagecontext-thread-safe)說UIGraphicsBeginImageContext是線程安全,這是否意味着我可以在任何線程上做UI ..context? –

0

您正在調用用戶界面代碼。 UI代碼必須位於主線程中,因爲該線程具有主運行循環和內容。

你需要做的是在主線程:

+(UIImage *)snapshot_of_view:(UIView*)view { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale); 
     [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return image; 
    }); 
} 
相關問題