2012-06-21 39 views

回答

4

在iOS7上,您可以使用新的[UIView snapshotViewAfterScreenUpdates:]方法。

爲了支持較舊的操作系統,您可以將任何視圖渲染到具有Core Graphics的UIImage中。我使用的UIView這一類的快照:

UView+Snapshot.h

#import <UIKit/UIKit.h> 

@interface UIView (Snapshot) 
- (UIImage *)snapshotImage; 
@end 

UView+Snapshot.m

#import "UIView+Snapshot.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation UIView (Snapshot) 

- (UIImage *)snapshotImage 
{ 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0); 
    [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return resultingImage; 
} 

@end 

它需要QuartzCore框架,所以一定要確保將它添加到您的項目。

使用,導入標題和:

UIImage *snapshot = [interestingView snapshotImage]; 
+0

Vytis,一切纔有意義在這裏除了'的UIImage *快照= [ interestingView snapshotImage];'Xcode抱怨:'使用未聲明的標識interestingView''。你究竟是如何申明的? – Greg

+1

'interestingView'是你想拍攝快照圖像的視圖。因此,例如,如果你想拍一個視圖控制器視圖的快照,你將在你的UIViewController代碼中有'UIImage * snapshot = [self.view snapshotImage];' 。 – Vytis

1

確實有可能,使用Core Graphics的渲染函數將視圖渲染到上下文中,然後使用該上下文的內容初始化圖像。請參閱this question的答案,以獲得一個好的技巧。

0

這裏是Vytis例如迅速2.x版

extension UIView { 

    func snapshotImage() -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0) 
     self.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     let resultingImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return resultingImage 
    } 
} 
相關問題