簡體   English   中英

CALayer renderInContext繪制空白圖像

[英]CALayer renderInContext draws a blank image

此代碼有效:

UIGraphicsBeginImageContextWithOptions(aRect.size, NO, 0.0);
[self.view drawViewHierarchyInRect:aRect afterScreenUpdates:YES];
anImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但我需要支持iOS 5和iOS6。我的Google搜索表示此代碼應該可以正常工作:

UIGraphicsBeginImageContextWithOptions(aRect.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
anImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但圖片為空白。 如何在iOS 5和6中拍攝視圖快照?

解決方案是縮放視圖以使其適合圖形上下文的邊界。 我發現的大多數示例都假定源視圖和目標上下文的大小相同。 我使用的圖形上下文比正在快照的視圖小得多,實際上它只是剪切了透明的視圖的一角。

UIGraphicsBeginImageContextWithOptions(aRect.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat scale = CGRectGetWidth(aRect) / CGRectGetWidth(self.view.bounds);
CGContextScaleCTM(ctx, scale, scale);
[self.view.layer renderInContext:ctx];
anImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM