我正在开发一个iOS5 iPhone应用程序,我想在用户按下故事板中视图的按钮时以编程方式截图。我尝试过很多代码,但它们都是针对iOs的老版本。我在iOS5里怎么做?谢谢!!
发布于 2011-11-28 02:11:27
- (void)drawRect:(CGRect)rect {
UIGraphicsBeginImageContext(self.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}发布于 2013-07-12 18:25:36
嗯,有几种方法可以通过编程来捕获iPhone屏幕
使用UIKIT http://developer.apple.com/library/ios/#qa/qa1703/_index.html
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();newImage窗口
UIGraphicsBeginImageContext(self.view.frame.size);renderInContext:UIGraphicsGetCurrentContext();*viImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext(); self.view.layer UIImage
在这5种方法中,我发现第一种方法对于复制-粘贴和应用压缩级别非常方便,因为第一种方法给出的图像是真实的像素数据。
发布于 2013-10-11 14:44:56
因为这是一个老问题,所以我把代码放在这里,希望能对大家有所帮助。(代码从苹果公司复制:http://developer.apple.com/library/ios/#qa/qa1703/_index.html)
- (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;
}https://stackoverflow.com/questions/8285889
复制相似问题