【问题标题】:How to capture a screenshot of ios simulator using code, and get the captured screenshot as a UIImage? [closed]如何使用代码截取ios模拟器的截图,并将截取的截图作为UIImage获取? [关闭]
【发布时间】:2013-08-28 01:59:39
【问题描述】:

要求是捕获显示为 UIImage 的屏幕内容,然后对该图像执行自定义关闭动画。

【问题讨论】:

  • 模拟器运行时按 Command + S 按钮。 :) 它会将完整的模拟器照片保存在桌面上。

标签: ios objective-c iphone uiimage


【解决方案1】:

这段代码将为您提供所传递视图的 UIImage。要获取整个屏幕的图像,请在您的一些 UIViewController 中传递 self.view

- (UIImage*)screenShotOfView:(UIView *)view
{
     UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
     [view.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext();

     return image;
}

【讨论】:

  • 我使用 UIgetScreenImage 和后来使用 UIView 动画块让它工作了 :) 无论如何谢谢!!
【解决方案2】:

第一个,captureView,返回一个 UIImage,其中包含任何 UIView 的渲染(EAGLView 除外,见下文)。如果您将应用的 UIWindow 传递给此方法,它将返回应用的屏幕截图。

第二种方法 saveScreenshotToPhotosAlbum 更进一步,将包含任何 UIView 渲染的图像保存到 iPhone 的相册中。

- (UIImage*)captureView:(UIView *)view 

{

        CGRect rect = [[UIScreen mainScreen] bounds];
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:context];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;

}



(void)saveScreenshotToPhotosAlbum:(UIView *)view 
{

        UIImageWriteToSavedPhotosAlbum([self captureView:view], nil, nil, nil);
}

【讨论】:

  • @Vamit:我使用 UIgetScreenImage 和后来使用 UIView 动画块让它工作了 :) 无论如何谢谢!!
【解决方案3】:

按 Command + Shift + 4 然后选择模拟器,快照将保存在您的桌面,然后将保存的图像拖到模拟器并在 safari 上打开...长选项卡保存图像...现在您可以从模拟器上的照片库中获取图像。

【讨论】:

  • 实际上我需要使用编码获取屏幕图像并使用该图像来执行自定义动画....请建议一些 api 或代码行@Islamahmed
  • 你必须图像上下文并开始在其上绘制图层...然后将其保存为图像...- (UIImage*)screenShotOfView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 保存使用此UIImageWriteToSavedPhotosAlbum([self captureView:view], nil, nil, nil);@lucas-eduardo 说...我认为这是唯一的方法
猜你喜欢
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 1970-01-01
相关资源
最近更新 更多