【问题标题】:Default iPhone screenshot programmatically以编程方式默认 iPhone 屏幕截图
【发布时间】:2015-04-13 23:25:47
【问题描述】:

我尝试了两种不同的方法来创建屏幕截图,但不幸的是它们不能按我的需要工作,我有一个 RMMapView 在屏幕截图上是空白的。当我在我的设备上手动创建快照时,它可以完美运行,并且地图视图在屏幕上。所以我想以编程方式实现相同的结果。正如我尝试的那样,有可能吗?或者这样做的正确方法是什么? (重现该类型的屏幕截图)

- (UIImage *) takeScreenshot {

    //1. version
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;


    //2. version
    UIGraphicsBeginImageContext(self.view.bounds.size);

    CGContextRef context=UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];

    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

}

【问题讨论】:

  • 什么时候调用takeScreenshot?
  • @MustafaIbrahim 点击按钮后。
  • 点击按钮时地图是否在屏幕上?
  • @MustafaIbrahim 当然。

标签: ios objective-c uiimage


【解决方案1】:

实际上,您可以在 RMMapView 中使用名为 takeSnapshot 的方法。

UIImage *image = self.mapView.takeSnapshot

[更新] 你可以试试这个方法吗

CGSize size = self.view.bounds.size;
CGRect cropRect = self.mapView.bounds

UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect);
UIImage * cropImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil);

UIGraphicsEndImageContext();

祝你好运

【讨论】:

  • 我知道这个解决方案,它返回的图像与当前视图不同,并且质量很差。这就是我想重现“手动”屏幕截图的原因。
  • 你是对的。 RMMapView Github repo github.com/mapbox/mapbox-ios-sdk/issues/589 上有一个未解决的问题,原因相同
  • 谢谢,我已经检查过了。可惜我没有时间,所以最快的方法是截图。
  • 不幸的是它仍然和我的尝试一样。
猜你喜欢
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
相关资源
最近更新 更多