Xcode4.2,SDK为5.0
已添加QuartzCore.framework。
#import <QuartzCore/QuartzCore.h>
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
-(UIImage*)getShot
{
CGSize imageSize = CGSizeMake(320, 460);
UIGraphicsBeginImageContext(imageSize);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
它在iOS5中运行良好,但在iOS4中运行不正确。
我是否使用
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0)
或
UIGraphicsBeginImageContext(imageSize)
没有帮助。
答案 0 :(得分:0)
- (UIImage*) takeScreenshot
{
if (iosVersionIsAtLeast(4.0))
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
else
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
// save to UIImage
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshot;
}