ios - 以编程方式默认 iPhone 屏幕截图

标签 ios objective-c uiimage

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

- (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;

}

最佳答案

实际上,您可以在 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();

祝你好运

关于ios - 以编程方式默认 iPhone 屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29616695/

相关文章:

ios - 为什么我的图像的每行字节数大于其每像素字节数乘以其宽度?

ios - 通过 NSDate 和 String 时间的混合对对象的 Swift 数组进行排序

ios - 应用商店更新 React Native 后本地存储是否清除?

objective-c - 参数是 KindOfClass : [NSNumber class] - sane way to check this?

objective-c - 当我将 UINavigationController 放入 UIPopoverController 中时,navigationBar 不显示

ios - UIImage 去我点击的地方(但我需要它所以你只能拖动它)

ios - 如何使用 Swift 3.0 播放附加到图像数组的声音?

ios - 无法编辑或关闭 CNContactViewController (iOS)

ios - 网络连接不良且 AVPlayer 正在播放时应用挂起

ios - 将 NSData 数组转换为 UIImage 数组