2010-08-16 85 views

回答

46

#import "QuartzCore/QuartzCore.h"您添加框架到項目後。然後執行:

UIGraphicsBeginImageContext(yourView.frame.size); 
[[yourView layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// The result is *screenshot 
+0

謝謝!那就是我正在尋找的東西。 (您錯過了「;」) – 2010-08-16 17:41:12

+0

謝謝,我更新了我的代碼:)! – elslooo 2010-08-16 20:27:01

2

我已經使用了答案來進一步改進它。不幸的是,原始代碼只生成鏡像圖像。

因此,這裏的工作代碼:

- (UIImage *) imageFromView:(UIView *)view { 

    UIGraphicsBeginImageContext(view.frame.size); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(currentContext, 0, view.size.height); 
    // passing negative values to flip the image 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    [[appDelegate.scatterPlotView layer] renderInContext:currentContext]; 
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return screenshot; 
}