Ios5 在iOs 5上以编程方式拍摄屏幕截图
我正在开发一个iOS5iPhone应用程序,我想在用户按下故事板中某个视图的按钮时以编程方式截图。我尝试过很多代码,但它们都是针对iOs的旧版本。我如何在iOs 5中做到这一点。谢谢 好吧,通过编程捕捉iPhone屏幕的方法很少Ios5 在iOs 5上以编程方式拍摄屏幕截图,ios5,screenshot,Ios5,Screenshot,我正在开发一个iOS5iPhone应用程序,我想在用户按下故事板中某个视图的按钮时以编程方式截图。我尝试过很多代码,但它们都是针对iOs的旧版本。我如何在iOs 5中做到这一点。谢谢 好吧,通过编程捕捉iPhone屏幕的方法很少 - (void)drawRect:(CGRect)rect { UIGraphicsBeginImageContext(self.bounds.size); [self.view.layer renderInContext:UIGraphics
- (void)drawRect:(CGRect)rect {
UIGraphicsBeginImageContext(self.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.window.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
在这5种方法中,我发现第一种方法对于复制粘贴和应用压缩级别非常方便,因为第一种方法提供了具有真实像素数据的图像。由于这是一个老问题,我将代码放在这里,希望能帮助到其他人。 (代码是从Apple复制的:)
@–的可能副本一二三 , 这篇文章明确地说用户希望它用于IOS5,UIGraphicsSendImageContext()在哪里?
- (UIImage*)screenshot
{
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
{
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [window center].x, [window center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [window transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y);
// Render the layer hierarchy to the current context
[[window layer] renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
}
}
// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}